我正在尝试创建自定义对话框。我遇到了2个编译错误,并没有真正理解为什么。
代码给出错误:
dialogButton.setOnClickListener(new OnClickListener() { // error message 1 here
@Override
public void onClick(View v) { // error message 2 here
dialog.dismiss();
}
});
所有代码
void StartGame()
{
// custom dialog
final Dialog dialog = new Dialog( cGlobals.MainHome );
dialog.setContentView(R.layout.activity_start_play);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
// ImageView image = (ImageView) dialog.findViewById(R.id.image);
// image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
答案 0 :(得分:0)
首先在顶部
定义final Context context = this;
然后改变
dialogButton.setOnClickListener(new OnClickListener() { // error message 1 hear
@Override
public void onClick(View v) { // error message 2 hear
dialog.dismiss();
}
});
到此:
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
和这个
final Dialog dialog = new Dialog( cGlobals.MainHome );
到
final Dialog dialog = new Dialog(context);