Refresh contents of dialog

后端 未结 2 377
孤街浪徒
孤街浪徒 2021-01-15 16:10

I need to update a custom dialog content whenever that dialog is ready to be drawin. I am not sure this could be refreshed directly or if I need to close it first and re-ins

相关标签:
2条回答
  • 2021-01-15 16:33

    Get the view that the Dialog is using and [post]invalidate it.

    Instead of Dialog.setContentView(int);

    Do something like:

    public class MyDialog {
            View v = null;
    
            public Dialog show(Context context) {
                Dialog d = new Dialog(context);
                v = LayoutInflater.from(context).inflate(R.layout.resource, null);
                d.setContentView(v);
                return d.show();
            }
    
            public void update() {
                v.invalidate();
            }
    }
    
    0 讨论(0)
  • 2021-01-15 16:40

    You can do everything in onPrepareDialog. It is called every time a dialog is about to be shown.

    void onPrepareDialog(final int id, final Dialog dialog) {
         switch(id) {
         case MY_DIALOG:
            TextView txtView = (TextView)dialog.findViewById(R.id.mytext);
            txtView.setText("My new text"):
            ....
         }
    }
    
    0 讨论(0)
提交回复
热议问题