处理外部类中的主要活动对话框按钮
问题描述:
我试图创建一个自定义对话框,按钮点击可能通过实例它的主要活动进行处理,有点像AlertDialog.Builder如何通过其setPositiveButton分配听众()和setNegativeButton()方法。处理外部类中的主要活动对话框按钮
这是我做了什么:
// THE MAIN ACTIVITY
public class main_code extends AppCompatActivity {
private commonModule comMod;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
comMod = new commonModule(this);
}
private void showDialog() {
DialogInterface.OnClickListener dialogHandler =
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// process "PROCEED" button
}
};
commonModule.myDialog customDialog = comMod.new myDialog();
customDialog.inputBox(this, "Submit Results",
"Your results will be submitted.", dialogHandler);
}
}
// THE COMMON MODULE CLASS
public class commonModule {
public commonModule(Context context){
this.context = context;
this.activity = (Activity) context;
}
public class myDialog {
public void showDialog(Activity activity, String title, String message,
DialogInterface.OnClickListener dialogHandler) {
final Dialog panel = new Dialog(activity);
panel.requestWindowFeature(Window.FEATURE_NO_TITLE);
panel.setContentView(R.id.customLayout);
panel.setCancelable(false);
TextView panelTitle = (TextView) panel.findViewById(R.id.title);
panelTitle.setTypeface(fontRes(PlayRegular));
panelTitle.setText(title);
TextView msgboxText = (TextView) panel.findViewById(R.id.content);
msgboxText.setTypeface(fontRes(PlayRegular));
msgboxText.setText(message);
Button button1 = (Button) panel.findViewById(R.id.button1);
button1.setTypeface(fontRes(PlayRegular));
button1.setText("ABORT");
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
panel.dismiss();
}
});
Button button2 = (Button) panel.findViewById(R.id.button2);
button2.setTypeface(fontRes(PlayRegular));
button2.setText("PROCEED");
button2.setOnClickListener(dialogHandler);
// error: View.OnClickListener cannot be applied to Dialog.OnClickListener
panel.show();
}
}
}
我尝试使用View.OnClickListener()为好,但没有成功。我希望对话框构建器是通用的和通用的,因此,对于每个对话框,点击侦听器应该是唯一的。
任何建议将非常感激。
TIA。
答
您DialogHandler中更改为View.OnClickListener
,而不是DialogInterface.OnClickListener
:
private void showDialog() {
View.OnClickListener dialogHandler =
new View.OnClickListener() {
@Override
public void onClick(View v) {
// process "PROCEED" button
}
};
commonModule.myDialog customDialog = comMod.new myDialog();
customDialog.inputBox(this, "Submit Results",
"Your results will be submitted.", dialogHandler);
}
和公共模块:
public void showDialog(Activity activity, String title, String message,
View.OnClickListener dialogHandler) {
...
button2.setOnClickListener(dialogHandler);
panel.show();
}
谢谢你,MARMOR。 *抓住我最后的评论。* **它的工作原理!** – iSofia