有没有办法使 EditText 在禁用 focusableInTouchMode 的情况下可聚焦? [英] Is there a way to make EditText focusable with disabled focusableInTouchMode?

查看:40
本文介绍了有没有办法使 EditText 在禁用 focusableInTouchMode 的情况下可聚焦?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 EditText,我想以编程方式控制它们的焦点顺序.但是,默认情况下,EditText 会在触摸时获得焦点,因此这会破坏顺序.

I have some EditTexts, and I want to control their focus order programmatically. However, EditText will get focus when touched by default, so this will break the order.

我曾尝试将 EditText 的 focusableInTouchMode 设为 false,但它从未获得焦点,就像它禁用了可聚焦一样.

I have tried to make EditText's focusableInTouchMode false, but it never got focus then, like it has disabled focusable.

那么,有没有办法禁用它的 focusableInTouchMode 但仍然可以聚焦?

So, there is a way to disable it's focusableInTouchMode but still focusable?

推荐答案

Alan 希望这是不可能的.解决方法是,创建一个 0dp 高和 0dp 宽的假编辑文本.

hope it's impossible Alan.Work around is ,Create one fake editText with 0dp hight and 0dp width.

      <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:fitsSystemWindows="true">
        <EditText
            android:id="@+id/edit_fake"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:focusable="true"
    
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            />
    
        <EditText
            android:id="@+id/edit_title"
            android:focusableInTouchMode="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
    
            android:layout_margin="32dp"/>
        <EditText
            android:id="@+id/edit_name"
            android:focusableInTouchMode="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/edit_title"
            app:layout_constraintStart_toStartOf="parent"
    
            android:layout_margin="32dp"/>
        <Button
            android:id="@+id/btn_done"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="done"
            app:layout_constraintTop_toBottomOf="@id/edit_name"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

//activity code


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

       fake=findViewById(R.id.edit_fake);
       name=findViewById(R.id.edit_name);
       title=findViewById(R.id.edit_title);
       done=findViewById(R.id.btn_done);

       fake.setOnFocusChangeListener(this);
       name.setOnFocusChangeListener(this);
       title.setOnFocusChangeListener(this);

       done.setOnClickListener(this);


    }


    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.btn_done){
            //clear focus
            name.clearFocus();
            fake.clearFocus();
            fake.requestFocus();

        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {

        if(v.getId()==R.id.edit_name){
            Log.d("onFocusChangename:", "name");
       }
        if(v.getId()==R.id.edit_fake){
            Log.d("onFocusChangenfake:", "fake");
        }

        if(v.getId()==R.id.edit_title){
            Log.d("onFocusChangenfake:", "tit");
        }
    }

这篇关于有没有办法使 EditText 在禁用 focusableInTouchMode 的情况下可聚焦?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆