单击菜单项上的警报对话框

时间:2018-10-10 08:58:10

标签: android android-alertdialog

代码

public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.name) {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(Profile.this);
        alertDialog.setTitle("Enter Name");
        final EditText input = new EditText(Profile.this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        input.setLayoutParams(lp);
        alertDialog.setView(input);
        alertDialog.setPositiveButton("Save",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        String mName = input.getText().toString();
                        mDatabaseReference.child("UserData").child(UID).child("Name").setValue(mName);
                        dialog.cancel();
                        Toast.makeText(getApplicationContext(),
                                        "Name successfully changed", Toast.LENGTH_SHORT).show();
                        }
                });
        alertDialog.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        alertDialog.show();

    }
    return super.onOptionsItemSelected(item);
  }
}

从来没有使用带有EditText的警报对话框,因此不确定该代码为何不起作用。我希望在菜单项单击上显示EditText

3 个答案:

答案 0 :(得分:0)

尝试以下代码:

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:orientation="vertical">

    <EditText
        android:id="@+id/edit1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text" />

</LinearLayout>

JAVA:

public void showChangeLangDialog() {
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    LayoutInflater inflater = this.getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.custom_dialog, null);
    dialogBuilder.setView(dialogView);

    final EditText edt = (EditText) dialogView.findViewById(R.id.edit1);

    dialogBuilder.setTitle("Custom dialog");
    dialogBuilder.setMessage("Enter text below");
    dialogBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            //do something with edt.getText().toString();
        }
    });
    dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            //pass
        }
    });
    AlertDialog b = dialogBuilder.create();
    b.show();
}

希望这对您有帮助

答案 1 :(得分:0)

尝试使用switch (item.getItemId())大小写而不是if。尝试以下提到的更改,这样我就可以根据需要看到Dialog

public boolean onNavigationItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.name:
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(Profile.this);
            alertDialog.setTitle("Enter Name");
            final EditText input = new EditText(Profile.this);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            input.setLayoutParams(lp);
            alertDialog.setView(input);
            alertDialog.setPositiveButton("Save",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            String mName = input.getText().toString();
                            mDatabaseReference.child("UserData").child(UID).child("Name").setValue(mName);
                            dialog.cancel();
                            Toast.makeText(getApplicationContext(),
                                            "Name successfully changed", Toast.LENGTH_SHORT).show();
                            }
                    });
            alertDialog.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
            alertDialog.show();
            return true;

    }
    return super.onOptionsItemSelected(item);
  }
}

答案 2 :(得分:0)

我想你下面的代码不见了

  

alertDialog.create()。show();

代替

  

alertDialog.show();