我试图在第一次打开应用程序时从用户那里获得价值。我膨胀一个AlertDialog来从用户那里获取值。尽管可以看到光标闪烁,但我无法在EditText中输入任何文本。

MainActivity中的代码

SharedPreferences sharedPreferences = getSharedPreferences("hasRunBefore", 0);
        boolean hasRun = sharedPreferences.getBoolean("hasRun", false);
        if (!hasRun) {
            SharedPreferences.Editor edit = sharedPreferences.edit();
            edit.putBoolean("hasRun", true);

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            LayoutInflater layoutInflater = LayoutInflater.from(this);
            final View dialogView = layoutInflater.inflate(R.layout.alertdialog, null);
            final EditText editText = (EditText) dialogView.findViewById(R.id.alert_dialog_editText);
            builder.setView(dialogView)
                    .setPositiveButton("Okay", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            editText.requestFocus();
                            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
                            regNum = editText.getText().toString();
                        }
                    })
                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
            builder.show();
            edit.putString("registerNumber", regNum);
            edit.apply();
            TextView textView = (TextView) findViewById(R.id.regNum_textView);
            textView.setText(regNum);
        } else {
            //code if the app HAS run before
            TextView textView = (TextView) findViewById(R.id.regNum_textView);
            textView.setText(regNum);
        }


布局文件是

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/alertDialog"
    >

    <TextView
        android:id="@+id/alert_dialog_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter Your Register Number"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="33dp" />

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/alert_dialog_editText"
        android:hint="Type your register number here"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_below="@+id/alert_dialog_textview"
         />

</RelativeLayout>


我究竟做错了什么?

最佳答案

尝试使用此方法:

public void showEditTextDialog()
{

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater layoutInflater = LayoutInflater.from(this);

    final View dialogView = layoutInflater.inflate(R.layout.alertdialog, null);
    final EditText editText = (EditText) dialogView.findViewById(R.id.alert_dialog_editText);

    // Keyboard
    final InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);

    // Auto show keyboard
    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean isFocused) {

            if (isFocused)
            {
                imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
            }
        }
    });

    builder.setView(dialogView)
            .setPositiveButton("Okay", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    regNum = editText.getText().toString();
                    Log.d("STACKOVERFLOW", "Registration number: " + regNum);

                    // Hide keyboard
                    imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    // Hide keyboard
                    imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

                    dialog.cancel();
                }
            });

    AlertDialog alertDialog = builder.create();
    alertDialog.show();
}


输出:

android - 无法在AlertDialog中编辑EditText-LMLPHP

10-08 09:10