如何显示一个alertdialog在其他alertdialog的顶部?当前景中的一个关闭时,背景中的一个应该是可见的



这是我的代码编辑/更新,我正在通过一个自定义alertdialog,其中包含从数据库获取的数据。现在,当提供的输入不正确时,我想在此基础上显示其他alertdialog,为用户提供一些消息。当用户关闭此消息alertdialog时,应该可以看到用于更新的前一个消息alertdialog。我该怎么做呢?

public class CountryEdit extends ListActivity{
    private Long mRowId;
    private CountryDbAdapter mDbHelper;
    public static final String PROVIDER_NAME = "assignment2.demos.MyCountriesCP";
    public static final String uriString = "content://"+ PROVIDER_NAME +"/countries";
    public static final Uri CONTENT_URI = Uri.parse(uriString);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ContentResolver contentResolver = getContentResolver();

        Cursor c = contentResolver.query(CONTENT_URI, null, null, null, getIntent().getExtras().getString("SORT_ORDER"));       
        String[] from = new String[] { "year", "country" };
        int[] to = new int[] { R.id.year, R.id.country };       
        SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.country_row,
                                        c, from, to);       
        setListAdapter(sca);
        mRowId = savedInstanceState != null ? savedInstanceState.getLong(assignment2.demos.MyCountriesActivity.KEY_ROWID) 
                : null;
        if (mRowId == null) {
            Bundle extras = getIntent().getExtras();            
            mRowId = extras != null ? extras.getLong(assignment2.demos.MyCountriesActivity.KEY_ROWID) 
                    : null;
        }
        populateFields();
    }

    private void populateFields() {
        LayoutInflater inflater=LayoutInflater.from(this);
        final View addView=inflater.inflate(R.layout.add_country, null);
        Cursor c = getContentResolver().query(CONTENT_URI.buildUpon().
                            appendPath(String.valueOf(mRowId)).build(), null, null, null, null);
        /* Read alert input */
        final EditText editCountry =(EditText)addView.findViewById(R.id.editCountry);
        final EditText editYear =(EditText)addView.findViewById(R.id.editYear);
            editCountry.setText(c.getString(c.getColumnIndex("country")));
            editYear.setText(c.getString(c.getColumnIndex("year")));
        new AlertDialog.Builder(this)
            .setTitle("Edit country/year")
            .setView(addView)
            .setPositiveButton("OK", 
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int whichButton) {
                            String country = editCountry.getText().toString();
                            if(country.trim().length()>0 && editYear.getText().toString().trim().length()>0){   
                                int year = Integer.parseInt(editYear.getText().toString());
                                ContentValues updateValues = new ContentValues();
                                updateValues.put(mDbHelper.COUNTRY, country);
                                updateValues.put(mDbHelper.YEAR, year);
                                getContentResolver().update(
                                            CONTENT_URI.buildUpon().appendPath(String.valueOf(mRowId)).build(), updateValues, null, null);
                                finish();
                            }
                            else{   
                                new AlertDialog.Builder(CountryEdit.this)
                                    .setTitle("Invalid Inputs!")
                                    .setMessage("You need to enter Country AND Year.")
                                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        // here you can add functions
                                        finish();
                                     }
                                  }).show();

//                              Toast.makeText(CountryEdit.this,
//                                      "You need to enter Country AND Year.", Toast.LENGTH_LONG).show();
                                //finish();
                            }
                        }
                    })
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int whichButton) {
                            // ignore, just dismiss
                            finish();
                        }
                    })
            .show();
    }
}

第一个警告对话框在第二个即将显示时自行消失(当单击侦听器完成时)。这是不可避免的。

有一个肮脏的hack,你可以重写由构建器创建的点击监听器,像这样:

create().getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener( ... }

但如果你不知道你在做什么,我不建议这样做(它可能会破坏互联网;-)。
你必须自己解散这个对话。如果你不这样做,它将永远不会关闭,用户将永远看到它,并且必须杀死你的应用程序才能重新启动。

最新更新