简体   繁体   中英

Change view on dialog when pressing a button in the dialog

Say i have two views in a layout - a Button and a TextView.

Is it possible to change the TextView text inside the button on click listener.

something like this:

 button.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            textview.settext("changedText");
        }
    });

The obvious error is that textview is not recognized by the OnClickListener method, and making it final will make it impossible for changing.

Making the textview final will still allow you to set the text. Only the assignment can be done once.

you are making final textView so you can not assign this reference to another object but you can do with any operation on that object.

you can change any property of textview thanx.

The final attribute is just restricting his initialization once. You can access is method after. (If that's what you need)

You can also make a field in your class and it will be available in the listener. Something like:

private TextView textview;

provide some ID to textview in the layout, create new variable TextView tv=new TextView(); in the same activity where dialogue is created

tv=(TextView)findviewbyid(R.id.ID_OF_TEXT_VIEW);

Then you should be able to use tv.settext("change text.")

declare textview at class level as below

Class CLASSNAME{

public TextView textview;

//your onclickListener code inside method or wherever u have written
}

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