我之前问过类似的问题。但是,当我尝试修改我的customDialog中定义的某个元素的句柄时,我的代码仍然崩溃可能重复:
Android - Getting the handle to components in a Custom Dialog
super.onCreate(savedInstanceState);
setContentView(R.layout.customdialog);
Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.customdialog);
dialog.setTitle("Send Message");
EditText phoneNumber = (EditText)dialog.findViewById(R.id.customDialogPhoneNumber1);
phoneNumber.setText("Hello");
如果我只删除phoneNumber.setText(“...”),代码将停止崩溃并显示对话框。但是,我确实需要从各种元素的句柄中获取东西。
知道我做错了吗?
答案 0 :(得分:0)
试试这个:
创建一个扩展对话框的类,并在其onCreate中执行setContentView和按钮实例化等。 例如:
public CustomDialog(Context context) {
super(context);
}
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b = (Button) findViewById(R.id.button_left);
}
public void setBListener(View.OnClickListener bListener) {
b.setOnClickListener(bListener);
}
在您的活动中:
CustomDialog d = construct your custom dialog
OnClickListener onBClick = construct your onclicklistener
d.setBListener(onBClick);