【问题标题】:The keyboard doesn't appear when I click on the EditText which is in the ListView当我单击 ListView 中的 EditText 时,键盘没有出现
【发布时间】:2018-01-17 13:25:03
【问题描述】:

我创建了一个带有 ListView 的警报对话框,ListView 包含一个 EditText,问题是当我单击 EditText 时,键盘不显示。请帮助我。这是我的代码:

MainActivity:

public class MainActivity extends AppCompatActivity {
View con_view;
ListView listView;
AlertDialog.Builder con_mBuilder;
MycustomAdapter mycustomAdapter;
ArrayList<ListItem> Item;
AlertDialog dialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void show_dialog(View view){
    con_view = getLayoutInflater().inflate(R.layout.dialog_layout, null);
    listView = con_view.findViewById(R.id.listtvv);

    Item = new ArrayList<ListItem>();
    Item.add(new ListItem("", "", ""));

    mycustomAdapter = new MycustomAdapter(Item);
    listView.setAdapter(mycustomAdapter);
    con_mBuilder = new AlertDialog.Builder(MainActivity.this);
    con_mBuilder.setView(con_view);
    dialog = con_mBuilder.create();
    dialog.show();
}


class MycustomAdapter extends BaseAdapter {
    ArrayList<ListItem> Item = new ArrayList<>();
    public MycustomAdapter(ArrayList Item) {
        this.Item = Item;
    }

    @Override
    public int getCount() {
        return Item.size();
    }

    @Override
    public String getItem(int i) {
        return Item.get(i).name;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LayoutInflater linflater = getLayoutInflater();
        View view1 = linflater.inflate(R.layout.row, null);
        return view1;
    }
}

}

这里是 警报对话框 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray">

<ListView
    android:id="@+id/listtvv"
    android:layout_width="230dp"
    android:layout_height="300dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</RelativeLayout> 

最后是包含 EditText 的视图的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/row_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_dark"
tools:layout_editor_absoluteY="81dp"
>

<EditText
    android:id="@+id/bomb"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:ems="10"
    android:inputType="textPersonName"
    android:selectAllOnFocus="true"
    android:text="Name"
    android:textColor="@android:color/white"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>

【问题讨论】:

  • 请分享此活动的 manifest.xml 代码
  • 你找到答案了吗? 3 年后,我在 MaterialDialog 中的 RecyclerView 遇到了同样的问题,哈哈。

标签: android listview


【解决方案1】:

您可以在 Layout xml 中的 EditText 中添加以下代码,

android:focusable="true"
android:focusableInTouchMode="true"

另外,您可以在代码中将其设置为,

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

或者,

 InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
 im.showSoftInput(edittext, 0);

【讨论】:

  • 我尝试了你所说的一切,但仍然面临同样的问题
  • dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);试试这个
  • 还是同样的问题
  • Dialog.getWindow().setSoftInputMode(WindowManager.LayoutPara‌​ms.SOFT_INPUT_STATE_‌​VISIBLE);尝试在上一条评论之后添加它。添加两行
  • dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);还是一样的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-25
  • 1970-01-01
  • 1970-01-01
  • 2015-11-22
  • 1970-01-01
  • 1970-01-01
  • 2013-04-20
相关资源
最近更新 更多