开发者

onClick method for programmatically created buttons

I've created an array of 5 clickable textviews using a loop, have set their parameters (size, colour, background image, to be clickable etc) and have set an onClickListener and the array is called "myArrayofTVs". Their ids have been set using the loop int (i). I have another predefined array that hold text string, and other textviews are present on the layout. Later on in the onClick method, and as all the buttons/clickable textviews do something very similar, I'd like to be able to do something like:

@Override
public void onClick(View v) { 

if(v == myArrayofTVs[i]) {               //using 'i' here doesn't seem to work
tv1.setText(myArray2[i]);
tv2.setText(myArray2[i+1];}
etc
etc}

I've tried various differnt ways such as using switch case statements (don't really want to use t开发者_如何学Pythonhese as there will be a lot of repeated code and I'll have to add a new case statement each time I want to add new textview/buttons in the future). Is there anyway of using one statement that will handle all the buttons/clickable textviews based on the variable id given or will I have to use a separate case/tag/id statement for each one?

Many thanks in advance!


Add the views to your ViewGroup and use getChildAt(int index) and getChildCount() to create a loop. You can loop all children/views in the viewgroup and you could check with

if(child instanceof TextView)

if they are of the correct type. Then you could cast the views back to a TextView/Button/View and do the thing you want to do.

But it sounds like you want a list of something. So i would suggest using a ListView with a adapter behind it.


I really think you should use the id provided by Android instead of trying to compare objects. The reason your code wouldn't work, if it had a sufficient for loop around it, is somewhat mysterious, but I would try to parallel the switch statements you see in examples as much as possible by comparing ID's and not objects.

for( int i = 0; i < myArrayofTvs.length; i++ )
    if(v.getId() == myArrayofTVs[i].getId()) {               
        tv1.setText(myArray2[i]); 
        tv2.setText(myArray2[i+1];
    }
}

Also obviously you'll want to avoid an array out of bounds error in that second inner statement.


What I did was programmatically inflate my custom layout and used an onClickListener on that button from the custom layout inflated. Then to interact with a specific item I got the parent view of the view being clicked eg. your button and then used that view to change attributes of the view. This is a snippet of my code. The onClick of the alertDialog is where I go about changing values of the newly inflated view.

            // if an edit button of numbers row is clicked that number will be edited
        if (view.getId() == R.id.NumberRowEditButton)
        {
            AlertDialog.Builder alert = new AlertDialog.Builder(this);

            alert.setTitle("Contact edit");
            alert.setMessage("Edit Number");

            // Set an EditText view to get user input
            final EditText input = new EditText(this);

            input.setSingleLine();
            alert.setView(input);

            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int whichButton)
                {
                    // get input
                    Editable value = input.getText();
                    if(value.length() > 4){

                        View tempView = (View) view.getParent();
                        TextView tempTV = (TextView) tempView.findViewById(R.id.numberRowTextView);
                        String number = tempTV.getText().toString();

                        tempTV.setText(value.toString());
                    }
                    else
                    {
                        // ...warn user to make number longer
                        final Toast msgs = Toast.makeText(ContactEdit.this, "Number must be over 4 digits.", Toast.LENGTH_SHORT);
                        msgs.setGravity(Gravity.CENTER, msgs.getXOffset() / 2, msgs.getYOffset() / 2);
                        msgs.show();
                    }
                }
            });

            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int whichButton)
                {
                    // cancel the dialog
                    dialog.cancel();
                }
            });

            alert.show();
        }

Hopefully this might help you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜