安卓飞机大战(三) 弹出对话框

在游戏时,不管是退出游戏还是选择战机,都要弹出一个对话框,需要以下代码


按一个按钮弹出对话框


Layout文件:(添加一个按钮)

<Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="准备起飞" />



在MainActivity 中:


public class choiceActivity extends Activity {
    private Button button1;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choice);
        button1=(ImageButton)findViewById(R.id.imageButton1);
        button2=(ImageButton)findViewById(R.id.imageButton2);
        button1.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                AlertDialog.Builder builder = new AlertDialog.Builder(choiceActivity.this);
                //    设置Title的图标
                builder.setIcon(R.drawable.ic_yxlm);
                //    设置Title的内容
                builder.setTitle("进入游戏");
                //    设置Content来显示一个信息
                builder.setMessage("确定选择进入游戏吗?");
                //    设置一个PositiveButton
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        Toast.makeText(choiceActivity.this, "进入游戏 ", Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent(choiceActivity.this,userActivity.class);
                        startActivity(intent);//跳转到下一个界面userActivity

                        finish();
                    }
                });
                //    设置一个NegativeButton
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        Toast.makeText(choiceActivity.this, "继续选择", Toast.LENGTH_SHORT).show();
                    }
                });
                builder.show();
                
            }
        });

}




这样就可以实现一个对话框了!

你可能感兴趣的:(游戏,dialog,安卓开发,对话框)