Android新API版本中的默认焦点行为

huangapple go评论90阅读模式
英文:

Android default focus behavior in new API versions

问题

以下是翻译好的内容:

我有一个简单的测试应用,只有一个EditText。当我在API 24的设备上运行它时,这个EditText在应用启动后默认是获得焦点的。但是当我在更新的Android API版本上运行它(比如API 28),应用启动后就没有任何获得焦点的视图。

以下是Java代码:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

以下是布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edtMain"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        android:hint="hint"/>
</androidx.constraintlayout.widget.ConstraintLayout>

以下是部分build.gradle文件:

compileSdkVersion 30
buildToolsVersion "30.0.2"

defaultConfig {
    applicationId "com.gmail.kapusta.yu.focustest"
    minSdkVersion 16
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

而且,当我将targetSdkVersion设置为26时,在API 28的设备上就会收到“旧”行为,即EditText在应用启动时会默认获得焦点。

所以,问题如下:

  1. 为什么不同版本中会有这样的行为?
  2. targetSdkVersion为30甚至更高(将来)时,我如何在新设备上获得默认在EditText上获得焦点的“旧”行为?

我知道可以在onResume()中调用requestFocus(),但这似乎不是一个好的方式。

英文:

I have a simple test app with a single EditText. When I run it on API 24 device, this EditText is focused by default after app starts. But when I run it on newer Android API versions (API 28, for example), I have no focused view after app starts.
Here is Java code:

public class MainActivity extends AppCompatActivity {

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

And here is layout:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.MainActivity&quot;&gt;

    &lt;EditText
        android:id=&quot;@+id/edtMain&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;
        android:hint=&quot;hint&quot;/&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

And here is a piece of my build.gradle:

compileSdkVersion 30
buildToolsVersion &quot;30.0.2&quot;

defaultConfig {
    applicationId &quot;com.gmail.kapusta.yu.focustest&quot;
    minSdkVersion 16
    targetSdkVersion 30
    versionCode 1
    versionName &quot;1.0&quot;

    testInstrumentationRunner &quot;androidx.test.runner.AndroidJUnitRunner&quot;
}

And, when I set targetSdkVersion to 26, I receive "old" behavior with EditText focused by default on API 28 device.

So, the questions are:

  1. Why do we have such a behavior in different versions?
  2. How can I get the "old" behavior with default focus on EditText on new devices when targetSdkVersion is 30 and maybe more (in future)?

I know that I can call requestFocus() in onResume(), for example, but it does not seem to be a good way.

答案1

得分: 1

官方文档确认,自targetSdkVersion 28起,视图不会隐式地获得焦点。因此,我们必须按照此处描述的方式明确指定焦点。详情请参阅此链接:https://stackoverflow.com/a/51950681/95313。

英文:

The official docs confirm that views are not implicitly focused since targetSdkVersion 28 https://developer.android.com/about/versions/pie/android-9.0-changes-28#focus. So we have to do this explicitly as described here https://stackoverflow.com/a/51950681/95313.

huangapple
  • 本文由 发表于 2020年9月23日 06:41:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64018611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定