EditText如何在启动时获取焦点并防止推动布局

4

我希望在活动启动时将我的EditText视图聚焦,因此我有以下代码:

<activity
    android:name=".TestViewActivity"
    android:windowSoftInputMode="stateVisible|adjustPan"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.DeviceDefault.NoActionBar.Fullscreen">
</activity>

这是一个在Button点击后启动的第二个活动。如果没有android:windowSoftInputMode="stateVisibleEditText在启动时将不会获得焦点,所以我加了这个属性。我的问题是,它确实获得了焦点,但它也将上面的TextView推了上去,即使使用了adjustPan。这是整个活动的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<EditText
    android:id="@+id/typeView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:id="@+id/wordsView"
    android:text="@string/hello_world"/>

</RelativeLayout>

我希望能够获得焦点并防止键盘将TextView向上推。

1个回答

1
请从清单文件中删除 android:windowSoftInputMode="stateVisible|adjustPan"。
在您的活动 onCreate() 中添加以下代码,
EditText myEditText = (EditText) findViewByID(R.id.typeView);
myEditText.requestFocus(); //Gets focus on Edit text.

或者您可以使用<requestFocus />在布局中请求焦点,就像这样: - mbonness

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接