在开发人员之间比较我们的设计时,我们发现了一个奇怪的行为。经过分析,我们进行了观察。
活动开始时,在某些情况下会显示键盘,但有时不显示。
实际上,如果不带ScrollView
,则默认情况下,软键盘不会显示在上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>
但是,当我们添加ScrollView
时,默认情况下会显示软键盘。
<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>
它仅取决于的存在ScrollView
。我们可以使用中的特定声明来解决该问题AndroidManifest
,但这是默认行为。
我和我的开发人员都奇怪为什么会这样?
这是我在深入研究Android代码并使用来构建一些测试布局之后对这个问题的理解EditText
。
如ScrollView
定义为
public class More ...ScrollView extends FrameLayout { ... }
我尝试使用aFrameLayout
作为项目的容器EditText
。结果,不会触发软件键盘。
<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>
但是正如问题中所写,使用ScrollView会触发软件键盘(我简化了xml源代码)。
<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>
因此,允许触发软件键盘的元素位于ScrollView
源文件中。
编辑:在创建了自己的MyFrameLayout
扩展FrameLayout的类并玩了代码之后,我发现它是默认滚动视图样式(R.attr.scrollViewStyle
)中负责显示或不显示键盘的内容...
Edit2:最后,该属性android:scrollbars
允许在启动时自动触发键盘(如果存在)...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句