温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-07-30 07:31:46 来源:网络 阅读:489 作者:JustMetU 栏目:开发技术

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


按一个按钮弹出对话框


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.p_w_picpathButton1);
        button2=(ImageButton)findViewById(R.id.p_w_picpathButton2);
        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();
                
            }
        });

}




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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI