Android EditText 没有聚焦

标签 android android-layout

这是我的问题,我有一个非常简单的布局,显示 2 个 EditText,一个用于标题,另一个用于描述。这是 xml 文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:weightSum="1">

    <EditText
            android:layout_width="match_parent"
            android:layout_height="63dp"
            android:id="@+id/title_field"
            android:background="#ffffffff"
            android:hint="@string/titleFieldHint"
            android:padding="20dp"
            android:textSize="8.5pt"
            android:maxLines="1"/>

    <View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="@android:color/darker_gray"/>

    <EditText
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/description_field"
            android:hint="@string/descriptionHint"
            android:gravity="top"
            android:padding="20dp"
            android:scrollbars="vertical"
            android:textSize="8.5pt"
            android:background="#ffffff"
            android:inputType="textMultiLine"
            android:textIsSelectable="true"
            android:focusableInTouchMode="true" android:layout_weight="1"/>

</LinearLayout>

当我使用此布局启动 Activity 时,键盘显示聚焦 title_field,因此我可以编写并单击其他字段(键盘仍处于打开状态)并编写描述。但是,如果我隐藏键盘,除非我单击 title_field,否则它不会再次显示(键盘),因此 description_field 不会响应,此外 title_field 支持开箱即用的选择(包括上下文操作栏),而 description_field 不允许我甚至选择。

我刚开始 android 开发,有什么我遗漏的吗?

最佳答案

如果您想在搜索 Activity 等特定 Activity 开始时使用键盘,请在您的 list 中使用此代码 -

注意 - 一旦用户登陆 Activity ,它就会弹出键盘......

<application ... >
<activity
    android:windowSoftInputMode="stateVisible" ... >
    ...
</activity>
...

您也可以在您的 java 文件中执行此操作。使用 Java,您可以更好地控制键盘外观。这是它的代码-

public void showSoftKeyboard(View view) {
if (view.requestFocus()) {
    InputMethodManager imm = (InputMethodManager)
            getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
    }
}

只需调用方法 showSoftKeyboard() 即可弹出键盘!!

希望对你有帮助!! 干杯!

Refrence Android Development Article

关于Android EditText 没有聚焦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28181184/

相关文章:

android - 卸载应用程序后谷歌游戏服务数据消失

Android:如何与本地服务通信和交换对象?

java - 通过 JSON 检索 ImageView 中的图像

java - 自定义字体在 Fragment Android Studio 中不起作用

android - 在 Android 中扩展 fragment

android - 如何刷新 ListView 控件;

android - SQLCipher Loadlibs 和数据库密码更改

java - 有没有办法检查概览按钮?

java - 新 Activity 没有响应

java - ListView 滚动后报错