英文:
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
在应用启动时会默认获得焦点。
所以,问题如下:
- 为什么不同版本中会有这样的行为?
- 当
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:
<?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>
And here is a piece of my 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"
}
And, when I set targetSdkVersion to 26, I receive "old" behavior with EditText focused by default on API 28 device.
So, the questions are:
- Why do we have such a behavior in different versions?
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论