【问题标题】:EditText field in AlertDialog not workingAlertDialog 中的 EditText 字段不起作用
【发布时间】:2014-07-08 16:34:11
【问题描述】:

我是 android 新手,正在尝试构建一个涉及一些简单数据输入和管理的简单应用程序。当我按照指南here 进行操作时,它会通过警报对话框要求用户输入。一切正常,除了出现 AlertDialog 后,EditText 字段没有显示输入光标,并且不会显示/更新我输入的文本。但是,在警报对话框中按下“确定”按钮后,我可以获得正确输入的文本字符串。这是涉及该对话框的 xml 文件和代码:

xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Type Your Message : "
    android:textAppearance="?android:attr/textAppearanceLarge" />

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

</EditText>

代码:

Button grade_in = button;

  grade_in.setTag(tag);
  grade_in.setOnClickListener(new OnClickListener(){

        public void onClick(View btn) {

            LayoutInflater inflater = LayoutInflater.from(activity.getApplicationContext());
            View prompt_grade_in = inflater.inflate(R.layout.grade_in, null);

            final EditText input_field = (EditText)prompt_grade_in.findViewById(R.id.grade_in);

            AlertDialog.Builder alertBuilder = new AlertDialog.Builder(activity);

            alertBuilder.setView(prompt_grade_in);

            alertBuilder.setCancelable(true)
                        .setPositiveButton("OK", 
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        // TODO Auto-generated method stub
                                        Log.v(null, input_field.getText().toString());

                                    }
                                })

                        .setNegativeButton("Cancel", 
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.cancel();

                                    }
                        })

                        .setMessage("Grade for the course:");

            AlertDialog alert = alertBuilder.create();
            alert.show();

        }
  });

我尝试添加一个 TextWatcher,但它没有帮助。版本是这个问题的根源吗?因为我认为我几乎按照指南进行了操作,只是做了一些小改动。感谢您的帮助!

附言包含按钮调用输入对话框的页面是一个片段。

更新: 包含按钮的片段由一个ExpandableListView 组成,它是根据这个guide 实现的(第15 部分)。上面实现警报对话框的代码在一个自定义适配器类中,它在列表中创建组项,在以下函数中:

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, final ViewGroup parent) {

if (convertView == null) {
  convertView = inflater.inflate(R.layout.listrow_group, null);
}

CheckedTextView checkedTextView = (CheckedTextView) convertView.findViewById(R.id.textView1);

course courses = (course) getGroup(groupPosition);

((CheckedTextView) checkedTextView).setText(courses.code + " - " + courses.title);
((CheckedTextView) checkedTextView).setChecked(isExpanded);

String tag = courses.code + " - " + courses.title;

CheckBox checkbox = (CheckBox) convertView.findViewById(R.id.checkbox_course);
checkbox.setChecked(sharedPref.getBoolean(tag, false));
setBoxListener(checkbox, tag);

Button button = (Button) convertView.findViewById(R.id.grade_button);
setBtnListener(button, tag);

return convertView;
}

【问题讨论】:

  • 刚试过,但问题仍然存在。我认为该主题的创建者正在处理输入法/键盘的一些问题?
  • alert.show();之后尝试input_field.requestFocus();
  • 我的朋友识别并修复了,原来这是一个小错误: LayoutInflater inflater = LayoutInflater.from(activity.getApplicationContext());相反,它应该是: LayoutInflater inflater = LayoutInflater.from(activity);我以为我从一开始就在使用活动。 :(

标签: android android-edittext android-alertdialog


【解决方案1】:

尝试将android:textCursorDrawable="@null" 设置为EditText 以进行输入光标显示

【讨论】:

  • 感谢您的建议,但问题仍然存在。 :(
  • 如果 cursorVisible 和 background 属性分别设置为 true 和 @null,EditText 字段会消失(不显示)。
  • @alanlo 发布更多代码以便更好地理解。
【解决方案2】:

将 xml 中的 EditText 代码替换为以下代码。以下代码包含 标记,该标记在 EditText 中获得焦点。希望这能解决您的问题。

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

        <requestFocus />

    </EditText>

【讨论】:

  • 感谢您的建议,但问题仍然存在。
【解决方案3】:

在创建 AlertDialog 之前调用软键盘。

alert.getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

【讨论】:

    【解决方案4】:

    被朋友识别并修复,原来是一个小错误:

    LayoutInflater inflater = LayoutInflater.from(activity.getApplicationContext());
    

    应该是:

    LayoutInflater inflater = LayoutInflater.from(activity);
    

    我以为我从一开始就在使用活动。 :(

    【讨论】:

      【解决方案5】:

      首先检查您的导入。

      如果您使用的是androidx. 使用:

      androidx.appcompat.app.AlertDialog

      而不是

      导入 android.app.AlertDialog;

      检查输入类型

      【讨论】:

        【解决方案6】:

        我也遇到过这个问题...在我的情况下:我使用了自定义对话框,在我的 dialog_custom.xml 中扩展了 DialogFragment / AppCompatDialogFragment 和 EdidText;

        我在对话框类的 onStart 中添加了以下代码,EditText 运行良好:

        override fun onStart() {
             super.onStart()
             dialog!!.window!!.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-09-03
          • 2012-10-01
          • 1970-01-01
          • 2012-10-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多