【问题标题】:Do not apply requestFocus on EditText that is inside CarouselView不要在 CarouselView 内的 EditText 上应用 requestFocus
【发布时间】:2018-09-28 12:26:29
【问题描述】:

我在RecyclerView 项目中有一个EditText,一旦选择了该项目,我想显示键盘并专注于EditText,但我什么都不做。

我使用 CarouselView 库,与 RecyclerView 相同。

活动中:

listStudent.setOnItemSelectedListener(new CarouselView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(CarouselView carouselView, int position, int adapterPosition, RecyclerView.Adapter adapter) {

                try {
                      View view = listStudent.getLayoutManager().findViewByPosition(listStudent.getCurrentAdapterPosition());
                      final EditText ed = ButterKnife.findById(view, R.id.edScore);
                      ed.requestFocus();

                      ed.post(new Runnable() {
                           @Override
                           public void run() {
                              InputMethodManager imm = (InputMethodManager)
                              AppController.Context.getSystemService(Context.INPUT_METHOD_SERVICE);
                              imm.showSoftInput(ed, InputMethodManager.SHOW_IMPLICIT);
                      }});

                 } catch (Exception ex) {}
            }

            @Override
            public void onItemDeselected(CarouselView carouselView, int position, int adapterPosition, RecyclerView.Adapter adapter) {

            }
     });

在 XML 子项中

<EditText
            android:id="@+id/edScore"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@xml/layout_border_color_primary"
            android:digits="0123456789."
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center"
            android:lines="1"
            android:maxLength="5"
            />

【问题讨论】:

    标签: android android-recyclerview android-edittext setfocus


    【解决方案1】:

    只需在你的 try 块中添加

    ed.setFocusableInTouchMode(true);
    

    【讨论】:

    • 非常感谢您的回答,但是如果您看,这段代码是用xml编写的。是不足够的?但是我添加了相同的代码但没有工作。
    • imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);添加这个
    • 没有用。您认为有不同的方式,甚至还有其他库可以以相同的方式使用吗?
    猜你喜欢
    • 2019-01-01
    • 2014-01-21
    • 2016-12-05
    • 2016-09-14
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    • 2013-06-08
    • 2018-03-12
    相关资源
    最近更新 更多