开发者

how to get value from a custom dialog box to the main activity in android app

开发者 https://www.devze.com 2023-03-23 17:18 出处:网络
in my activity when the user clicks a t开发者_如何学Cextview a custom dialog box is opened. Here i have set some wheel pickers. The value which the user sets in the wheel picker is been shown as a tit

in my activity when the user clicks a t开发者_如何学Cextview a custom dialog box is opened. Here i have set some wheel pickers. The value which the user sets in the wheel picker is been shown as a title in the custom dialog box. When the user clicks the Ok button the dialog box closes and i want the that title text to be displayed in my main activity's text view.

I know that we can set initialize a variable in our activity class and set it's value in dialog box, nothing special. But i have written the Custom dialog box as a separate class file as follows

bd = (TextView)findViewById(R.id.editText1);
        bd.setOnClickListener(new OnClickListener() 
        {   
            @Override
            public void onClick(View v) 
            {
                CustomizeDialog customizeDialog = new CustomizeDialog(main.this);
                customizeDialog.show();
            }
        });    

How to get the value from that class to my main activity class...


Create an handler in your Activity and pass it to your dialog that will do the job. Something like below

Handler mHandler = new Handler()
{
    public void handleMessage(android.os.Message msg)
    {
        super.handleMessage(msg);

        switch (msg.what)
        {
        case 1:
            // Do some stuff
            break;
        }
    }
}

Now pass this handler object

CustomizeDialog customizeDialog = new CustomizeDialog(main.this, mHandler);
customizeDialog.show();

When you want set some value send it using this handler object


I found a better way, in my main activity i have assigned the TextView as

public static TextView bd;

In my CustomizeDialog class when i initialized a variable as follow

static String bdDate;

When i click the Ok button to close the the Custom Dialog, i have written as

close.setOnClickListener(new View.OnClickListener()
         {  
            @Override
            public void onClick(View v) 
            {
                  if (v == close)
                  bdDate = wheelMenu3[getWheel(R.id.p3).getCurrentItem()];
                  main.bd.setText(bdDate);
                  dismiss();
            }
        });
0

精彩评论

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