gpt4 book ai didi

带有按钮onClick事件的android自定义对话框

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:03:16 28 4
gpt4 key购买 nike

我有这样的 CustomDialog.java :

public class CustomDialog {
Dialog dl;
public void ShowDialog(Context context, String message) {
dl = new Dialog(context);
dl.setContentView(R.layout.custom_dialog);

TextView tv_message = (TextView) dl.findViewById(R.id.textViewMessage);

tv_message.setText(message);

Button bt_yes = (Button)dl.findViewById(R.id.buttonYes);
Button bt_no = (Button)dl.findViewById(R.id.buttonNo);

bt_yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckYes();
}
});
bt_no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dl.dismiss();
}
});
dl.show();

}

public Boolean CheckYesNo(Boolean check){
return check;
}

public Boolean CheckYes() {
return true;
}

public void CloseDialog() {
dl.dismiss();
}

这是 Activity 中使用 CustomDialog 的代码:

CustomDialog cdl = new CustomDialog ();
cdl.ShowDialog(Activity1.this, "test");
if (cdl.CheckYesNo(true)) {
// doing something with data and go to Activity 2
}
else {
cdl.CloseDialog();
}

我想要这样:
1. 在Activity1中,点击ImageButton时,会显示CustomDialog。
2. CustomDialog 显示后,如果单击 Button yes,它会处理数据并转到 Activity2。
3. 如果单击 Button no,CustomDialog 将关闭并且不会对数据进行任何操作。

但我的问题是:
当单击 ImageButton 时,CustomDialog 显示,我想处理数据的代码将执行并自动转到 Activity2。
我无法选择是或否来点击。
我认为 Button 中的问题是 onClick 事件。
如何解决?

最佳答案

parameter initilize

  Dialog dialog;

You use this where You want to use

dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog);
dialog.show();


TextView tv_message = (TextView) dialog .findViewById(R.id.textViewMessage);

tv_message.setText(message);

Button bt_yes = (Button)dialog.findViewById(R.id.buttonYes);
Button bt_no = (Button)dialog.findViewById(R.id.buttonNo);

bt_yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckYes();
}
});
bt_no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});

关于带有按钮onClick事件的android自定义对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38476045/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com