代码之家  ›  专栏  ›  技术社区  ›  MojioMS

android编辑文本点击按钮后删除焦点

  •  24
  • MojioMS  · 技术社区  · 13 年前

    我有一个带有编辑文本和按钮的活动。 当用户点击EditText时,键盘就会显示出来,他可以输入一些Text-fine。 但当用户点击按钮时,我希望EditText不再处于焦点位置,即键盘隐藏,直到用户再次点击EditText。

    单击按钮后,我可以做些什么来“隐藏EditText的焦点”。 我可以在按钮的OnClick方法中添加一些代码来做到这一点?

    编辑:

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    
        <EditText 
            android:id="@+id/edt_SearchDest"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.8"
            android:textSize="18sp"
            android:hint="Enter your look-up here.." />
    
        <Button
            android:id="@+id/btn_SearchDest"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:text="Search" />
    
    </LinearLayout>
    

    顺致敬意,

    12 回复  |  直到 12 年前
        1
  •  45
  •   pinckerman Gordon Linoff    12 年前

    把这个放在你的按钮监听器中:

    InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
    
    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
    

    编辑

    如果没有,上面的解决方案将破坏您的应用程序 EditText 专注于。如下修改您的代码:

    将此方法添加到类中:

    public static void hideSoftKeyboard (Activity activity, View view) 
    {
        InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
    }
    

    然后,在按钮监听器中,调用如下方法:

    hideSoftKeyboard(MainActivity.this, v); // MainActivity is the name of the class and v is the View parameter used in the button listener method onClick.
    
        2
  •  16
  •   Community Mohan Dere    7 年前

    一种解决方法是创建一个假视图,以便在您 clearFocus 在您的 edittext :

    <EditText
            android:id="@+id/edt_thief"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:focusable="true"
            android:focusableInTouchMode="true"
    

    请注意,此视图是不可见的,因此在布局中不需要任何空间。

    在控件类中,可以添加如下方法来触发此焦点转移:

    public void clearFocus(){
            yourEdittext.clearFocus();
            edtThief.requestFocus();
        }
    

    然后可以最小化键盘一次 edtThief 有重点:

     public static void hideKeyboard(final View view) {
        InputMethodManager imm = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
    
        3
  •  11
  •   pinckerman Gordon Linoff    12 年前

    我已经成功地在onClick按钮代码中使用了以下内容:

    editText.setEnabled(false);
    editText.setEnabled(true);
    

    比其他方法稍微复杂一些。。。

        4
  •  8
  •   Marjan Slavkovski    9 年前

    我能找到的最优雅的解决方案是:

    您可以将此方法保存在实用程序类中:

    public static void hideSoftKeyboard(Activity activity) {
       if (activity == null) return;
       if (activity.getCurrentFocus() == null) return;
    
       InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
       inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    }
    

    通过简单地调用hideSoftKeyboad()方法,它将隐藏键盘,但正如您所看到的,焦点仍然存在。

    为了消除焦点,我们将使用一个简单的技巧。在输入控件的正上方,添加一个伪视图,如下所示:

    <View
        android:id="@+id/dummy"
        android:layout_width="1dp"
        android:layout_height="1dp"
        android:focusable="true"
        android:focusableInTouchMode="true" />
    

    然后,在调用焦点隐藏方法的地方写下这行代码:

     theTextView.clearFocus();
    

    由于应用程序需要将焦点传递给下一个控件,因此它将传递给我们的不可见视图。

        5
  •  1
  •   Bukunmi    7 年前

    我是怎么解决的。

    // xml file
    <LinearLayout
    ...
    android:id="@+id/linear_layout"
    android:focusableInTouchMode="true"> // 1. make this focusableInTouchMode...
    </LinearLayout>
    
    // Activity file
    private LinearLayout mLinearLayout; // 2. parent layout element
    private Button mButton;
    
    mLinearLayout = findViewById(R.id.linear_layout);
    mButton = findViewById(R.id.button);
    
      mButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mLinearLayout.requestFocus(); // 3. request focus
    
                }
            });
    

    我希望这能帮助你:)

        6
  •  1
  •   PhantomCosmos    6 年前

    最重要的答案肯定有效,但在我的用例中会添加很多不必要的代码,因为我的用例有很多按钮,每个按钮都需要一个setOnClickListener代码块来从EditText中删除焦点。

    相反,我的方法是编写BindingAdapter来执行焦点更改和预期的单击操作。

    绑定适配器

    @BindingAdapter("onClickWithFocusChange")
    fun View.setOnClickWithFocusChangeListener(clickListener: View.OnClickListener?) {
        clickListener?.also {
            setOnClickListener(OnClickWithFocusChangeListener(it))
        } ?: setOnClickListener(null)
    }
    
    class OnClickWithFocusChangeListener(
        private val clickListener: View.OnClickListener
    ) : View.OnClickListener {
    
        override fun onClick(v: View?) {
            v?.requestFocusFromTouch()
            clickListener.onClick(v)
            v?.clearFocus()
        }
    }
    

    在xml中(现在可以使用数据绑定,而不是以编程方式设置每个点击监听器):

    <!-- parentview of the EditText -->
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true">
    
        <ImageButton
        ...
        onClickWithFocusChange="@{() -> viewModel.buttonClicked()}"
        ... />
    

    在活动/片段中:

    editText.setOnFocusChangeListener { v, hasFocus ->
        if (!hasFocus) {
            requireContext().hideKeyboard(v)
            v.clearFocus()
        }
    }
    

    最后是扩展函数:

    fun Context.hideKeyboard(view: View) {
        val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
    }
    

    希望这能帮助到一些人!

        7
  •  1
  •   manjunatha poojari    4 年前
    <LinearLayout 
        android:id="@+id/parent"
        android:focusableInTouchMode="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    
        <EditText 
            android:id="@+id/edt"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.8"
            android:textSize="18sp"
            android:hint="Enter your look-up here.." />
    
        <Button
            android:id="@+id/btn"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:text="Search" />
    
    </LinearLayout>
    
    1. 设置 android:focusableInTouchMode="true" 到父布局。
    2. 单击按钮,将焦点转移到父对象。
        binding.btn.setOnClickListener {
                binding.parent.requestFocus()
                // your stuff here
            }
    
        8
  •  0
  •   pinckerman Gordon Linoff    12 年前
    private void hideDefaultKeyboard() {
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);//u have got lot of methods here
    }
    

    编辑:

    LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
    
        9
  •  0
  •   Taryn Frank Pearson    12 年前
    mTextView.setEnabled(!mTextView.isEnabled());
    mTextView.setEnabled(!mTextView.isEnabled());
    
        10
  •  0
  •   Chaitanya Atkuri    9 年前

    很抱歉回答晚了,但我希望这是正确的答案,因为我用

    try {
    InputMethodManager imm = (InputMethodManager) context
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
    if (v != null) {
    imm.hideSoftInputFromWindow(v.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
    }
    } catch (Exception ignored) {
    }
    mEditText.setSelected(false);
    mEditText.setFocusable(false);
    mEditText.setFocusableInTouchMode(true);
    

    在点击按钮时写下以下片段。

        11
  •  -1
  •   brbaker    13 年前

    为什么不禁用Button代码中的EditText?这应该可以去掉键盘和焦点。

    edt_SearchDest.setEnabled(false);

        12
  •  -1
  •   Max Pethel    6 年前

    如果你试图像在笔记应用程序中那样创建按钮,只需执行以下操作:

    note.setEnabled(false);
    note.setEnabled(true);
    

    这将产生一种类似复选标记的按钮(隐藏键盘并删除光标)。

    推荐文章