为什么软键盘显示或不活动启动时? [英] Why the soft keyboard shows or not when an activity starts?
问题描述
在比较我们的开发人员之间的设计中,我们发现了一个奇怪的现象。经过一番分析,我们就到这个观察。
When comparing our design between developers, we found a strange behavior. After some analysis we went to this observation.
当活动开始,在某些情况下,键盘的出现,但有时并非如此。
When the activity starts, on some cases the keyboard appears but sometimes not.
事实上,没有一个滚动型
,软键盘默认情况下不上出现的EditText
。
In fact, without a ScrollView
, the soft keyboard does not appear by default on an EditText
.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TestActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
</LinearLayout>
但是,当我们添加了一个滚动型
,软键盘默认情况下显示出来。
But when we add a ScrollView
, the soft keyboard shows up by default.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TestActivity" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
</ScrollView>
</LinearLayout>
这仅取决于滚动型
的presence。我们可以解决这个问题,在一个特定的声明 AndroidManifest
,但是这是默认的行为。
It only depends on the presence of the ScrollView
. We can fix that with a specific declaration in the AndroidManifest
, but this is the default behavior.
我和我的同事开发者想知道这是为什么发生?
I and my fellow developer wonder why is this occurring ?
推荐答案
下面是我挖的Android code和建设一些测试布局与的EditText <后明白这个问题, / code>。
Here is what I understand of this problem after digging in the code of Android and building some test layouts with an EditText
.
由于滚动型
定义为
public class More ...ScrollView extends FrameLayout { ... }
我试图用一个的FrameLayout
作为容器的的EditText
项。其结果是该软件键盘未被触发。
I tried using a FrameLayout
as a container for an EditText
item. As a result the software keyboard is not triggered.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="text" >
<requestFocus />
</EditText>
</FrameLayout>
但写在的问题,采用了滚动型触发软键盘(我简化了XML源)。
But as written in the question, using a ScrollView triggers the software keyboard (I simplified the xml source).
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="text" >
<requestFocus />
</EditText>
</ScrollView>
这样可以让软键盘被触发的元素是滚动型
源文件中的
编辑:在已经创建了自己的类 MyFrameLayout
延伸的FrameLayout,并与code打,我发现这东西在默认的滚动视图样式( R.attr.scrollViewStyle
),它负责键盘显示或不...
after having created my own class MyFrameLayout
extending FrameLayout and playing with the code, I found that it is something in default scrollview style (R.attr.scrollViewStyle
) that is responsible for the keyboard to be shown or not...
EDIT2:终于属性安卓滚动条
使键盘自动触发启动时,如果present ...
finally the attribute android:scrollbars
allows the keyboard to be automatically triggered at startup if present...
这篇关于为什么软键盘显示或不活动启动时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!