如何创建提示对话

问题描述 投票:0回答:2
ListView menuList = (ListView) findViewById(R.id.ListView_Menu);
 String[] items = {getResources().getString(R.string.menu_item_NxtInc)};
 ArrayAdapter<String> adapt = new ArrayAdapter<String>(this,R.layout.menu_item,items);
 menuList.setAdapter(adapt);
 menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
     public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) {
              TextView textView = (TextView) itemClicked;
              String strText = textView.getText().toString();
              if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_NxtInc))) {
                  startActivity(new Intent(EndActivity.this, NxtIncActivity.class));
                  EndActivity.this.finish();
              } 

我已经使用上面的代码在我的应用程序中创建了一个菜单。如果用户单击字符串menu_item_NxtInc,则会将它们带到下一个活动。我想要发生的是提示出来说你确定要这么做吗?任何人都知道如何实现这一点。谢谢

android dialog prompt
2个回答
6
投票

就这么简单

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to do this baby?")
       .setCancelable(false)
       .setPositiveButton("Yuss, lets do this", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                // fire an intent go to your next activity
           }
       })
       .setNegativeButton("Err, no", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
           }
       });
AlertDialog alert = builder.create();
alert.show();

0
投票

在片段中this通过getActivity()。所以它会变成这样的东西。

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
© www.soinside.com 2019 - 2024. All rights reserved.