简体   繁体   中英

Android set button pressed on click

I am dynamically creating a table of ImageButton, I would like to have setPressed to true for the image button. when it's clicked by the user. Since I have created the buttons dynamically, how do I setPressed using onClickListener?

        {
            ImageButton b = new ImageButton(this);
            b.setLayoutParams(new TableRow.LayoutParams(
                                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            b.setBackgroundResource(R.drawable.happy);
            b.setOnClickListener(new OnClickListener() {
                            public void onClick(View v) {
                                onClickListenerForTag("tagStr");
                            }
            }
        }

    private void onClickListenerForTag(final String tagStr) {
            assert (tagStr != null);
                    mTagStr = tagStr;
                    //need to call setPressed(true) here ?

        }

The View being passed in to the handler is the button instance. Cast the Button and call the method.

public void onClick(View v) {
     onClickListenerForTag("tagStr", v);
    }

private void onClickListenerForTag(final String tagStr, View v) {
            assert (tagStr != null);
            mTagStr = tagStr;

            ImageButton button = (ImageButton)v;
            button.setPressed(true);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM