2011-07-30 26 views
0

如何将值传递回自定义选择Dialog类的活动?...我有一个应用程序单击图像将出现一个对话框类,我将选择一个值..i需要更新后面活动中的这些值..可以有人建议我吗?自定义对话框在android中传递值

回答

0

如果您的对话框是一个主题声明为对话框的活动,您应该检查如何在活动中使用startActivityForResult();
,通过它可以从活动2如果您使用延伸对话框类数据传送到活动1.

3

,你可以添加一个接口,让想在单击对话框按钮时要执行的操作(如在调用活动中设置您的值),然后在您的构造函数中设置回调。事情是这样的:

final CustomDialog dialog = new CustomDialog(this, new ICustomDialogEventListener() { 
     public void customDialogEvent(int value) { 
      // Do something with the value here, e.g. set a variable in the calling activity 
     } 
    }); 

public class CustomDialog extends Dialog { 

    // this is your interface for what you want to do on the calling activity 
    public interface ICustomDialogEventListener { 
     public void customDialogEvent(int valueYouWantToSendBackToTheActivity); 
    } 

    private ICustomDialogEventListener onCustomDialogEventListener; 

    // In the constructor, you set the callback 
    public CustomDialog(Context context, 
      ICustomDialogEventListener onCustomDialogEventListener) { 
     super(context); 
     this.onCustomDialogEventListener = onCustomDialogEventListener; 
    } 

    // And in onCreate, you set up the click event from your dialog to call the callback 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     Button btnOk = (Button) findViewById(R.id.customDialogButton); 
     btnOk.setOnClickListener(new Button.OnClickListener() 
     { 
      public void onClick(View v) { 
       onCustomDialogEventListener.customDialogEvent(valueYouWantToSendBackToTheActivity); 
       dismiss(); 
      } 
     }); 
    } 
} 

当你想用你的对话框中,可使用设置在您的电话活动的价值回调构建它