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

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

Android default focus behavior in new API versions

问题

以下是翻译好的内容:

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

以下是Java代码:

  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. }
  7. }

以下是布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <EditText
  9. android:id="@+id/edtMain"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. app:layout_constraintTop_toTopOf="parent"
  13. android:hint="hint"/>
  14. </androidx.constraintlayout.widget.ConstraintLayout>

以下是部分build.gradle文件:

  1. compileSdkVersion 30
  2. buildToolsVersion "30.0.2"
  3. defaultConfig {
  4. applicationId "com.gmail.kapusta.yu.focustest"
  5. minSdkVersion 16
  6. targetSdkVersion 30
  7. versionCode 1
  8. versionName "1.0"
  9. testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  10. }

而且,当我将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:

  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. }
  7. }

And here is layout:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;match_parent&quot;
  7. tools:context=&quot;.MainActivity&quot;&gt;
  8. &lt;EditText
  9. android:id=&quot;@+id/edtMain&quot;
  10. android:layout_width=&quot;match_parent&quot;
  11. android:layout_height=&quot;wrap_content&quot;
  12. app:layout_constraintTop_toTopOf=&quot;parent&quot;
  13. android:hint=&quot;hint&quot;/&gt;
  14. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

And here is a piece of my build.gradle:

  1. compileSdkVersion 30
  2. buildToolsVersion &quot;30.0.2&quot;
  3. defaultConfig {
  4. applicationId &quot;com.gmail.kapusta.yu.focustest&quot;
  5. minSdkVersion 16
  6. targetSdkVersion 30
  7. versionCode 1
  8. versionName &quot;1.0&quot;
  9. testInstrumentationRunner &quot;androidx.test.runner.AndroidJUnitRunner&quot;
  10. }

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:

确定