如何从Android的对话框启动活动对话框、Android

2023-09-06 15:34:25 作者:撩污酷野浪

我创建了一个自定义对话框,我想开始一个新的活动被点击时确定。我怎样才能获得的上下文将其设置为我的意图构造的第一个参数?

我可以创建使用的getContext()的意图,但我不能叫 startActivity 。难道我通过活动呼吁对话,以对话的构造?它是通常的方式通过单击对话框启动活动?

 公共类CustomDialog扩展对话框实现OnClickListener {
    按钮okButton,cancelButton;

    公共CustomDialog(上下文的背景下){
        超(上下文);
        的setContentView(R.layout.custom_dialog);
        okButton =(按钮)findViewById(R.id.button_ok);
        okButton.setOnClickListener(本);
        cancelButton =(按钮)findViewById(R.id.button_cancel);
        cancelButton.setOnClickListener(本);
    }

    @覆盖
    公共无效的onClick(视图v){
        如果(V == cancelButton)
            解雇();
        其他 {
            意图I =新的意图(的getContext(),ItemSelection.class);
            startActivity(ⅰ); //该方法startActivity(意向)是未定义CustomDialog类型
        }
    }
}
 

解决方案

 公共类CustomDialog扩展对话框实现OnClickListener {
  按钮okButton,cancelButton;
  活动mActivity;

  公共CustomDialog(活动活动){
    超(活动);
    mActivity =活动;
    的setContentView(R.layout.custom_dialog);
    okButton =(按钮)findViewById(R.id.button_ok);
    okButton.setOnClickListener(本);
    cancelButton =(按钮)findViewById(R.id.button_cancel);
    cancelButton.setOnClickListener(本);
  }

  @覆盖
  公共无效的onClick(视图v){
    如果(V == cancelButton)
        解雇();
    其他 {
        意图I =新的意图(mActivity,ItemSelection.class);
        mActivity.startActivity(ⅰ);
    }
  }
}
 

Android使用Activity实现简单的可输入对话框

I created a custom dialog and I'd like to start a new activity when OK is clicked. How can I get the context to set it as first argument of my Intent constructor?

I can create the intent using getContext(), but I can't call startActivity. Shall I pass the activity calling the dialog to the dialog's constructor? Is it the usual way to start an activity by clicking a dialog?

public class CustomDialog extends Dialog implements OnClickListener {
    Button okButton, cancelButton;

    public CustomDialog(Context context) {      
        super(context);     
        setContentView(R.layout.custom_dialog);
        okButton = (Button) findViewById(R.id.button_ok);
        okButton.setOnClickListener(this);
        cancelButton = (Button) findViewById(R.id.button_cancel);
        cancelButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {       
        if (v == cancelButton)
            dismiss();
        else {
            Intent i = new Intent(getContext(), ItemSelection.class);
            startActivity(i); //The method startActivity(Intent) is undefined for the type CustomDialog
        }
    }
}

解决方案

public class CustomDialog extends Dialog implements OnClickListener {
  Button okButton, cancelButton;
  Activity mActivity;

  public CustomDialog(Activity activity) {      
    super(activity);
    mActivity = activity;
    setContentView(R.layout.custom_dialog);
    okButton = (Button) findViewById(R.id.button_ok);
    okButton.setOnClickListener(this);
    cancelButton = (Button) findViewById(R.id.button_cancel);
    cancelButton.setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {       
    if (v == cancelButton)
        dismiss();
    else {
        Intent i = new Intent(mActivity, ItemSelection.class);
        mActivity.startActivity(i);
    }
  }
}