【发布时间】:2014-08-13 07:16:22
【问题描述】:
我的button Yes 和button No 在Backpressed 中的自定义对话框 上的功能存在问题。
这是我的代码:
public class menu extends Activity implements OnClickListener {
static int nextmaze;
Dialog dialog;
Button btnNo, btnYes;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
Button btnplay = (Button) findViewById(R.id.btnPlay);
Button btnHigh = (Button) findViewById(R.id.btnHighScores);
Button btnSettings = (Button) findViewById(R.id.btnSettings);
btnplay.setOnClickListener(this);
btnHigh.setOnClickListener(this);
btnSettings.setOnClickListener(this);
}
protected void showCustomDialog() {
dialog = new Dialog(this, android.R.style.Theme_Translucent);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.backpressdialog);
Button btnNo = (Button) dialog.findViewById(R.id.btnNo);
btnNo.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
Button btnYes = (Button) dialog.findViewById(R.id.btnYes);
btnYes.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
finish();
}
});
dialog.show();
}
@Override
public void onBackPressed() {
showCustomDialog();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSettings:
Intent prefs = new Intent(menu.this, Setting.class);
startActivity(prefs);
break;
case R.id.btnHighScores:
Intent surface = new Intent(menu.this, SurfaceViewExample.class);
startActivity(surface);
break;
case R.id.btnPlay:
startnextmaze();
}
}
我的项目似乎正在运行,但每次我按下对话框上的按钮是和按钮时,程序都会关闭。我认为dismiss function 不起作用?为什么?
更新:这是我的XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/bgdialog"
android:orientation="vertical" >
<Button
android:id="@+id/btnNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dialogno" />
<Button
android:id="@+id/btnYes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dialogyes" />
</RelativeLayout>
【问题讨论】:
标签: android xml eclipse dialog