Android EditText不显示键盘

8

我有一个屏幕上有三个EditText的界面,其中一个位于屏幕顶部的第一个EditText在我点击它时并不会调用键盘。我没有使用AVD,而是使用自己的设备,在其他设备上测试也是同样的结果。

我将在这里放置屏幕和xml代码... EditText之间没有任何区别,只是位置不同。要在该EditText中输入,我需要选择上面的一个EditText,然后键盘就会出现。至少当我再次选择第一个EditText时它不会隐藏。

我已经尝试过setFocus(true),但它并不能解决我的问题。请帮帮我!

XML

<RelativeLayout 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"
android:background="@drawable/background"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SaldoCadastro" >

<TextView
    android:id="@+id/tvTituloReg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Saldo Banco"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/tvErroSenha1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/tvTituloReg"
    android:layout_marginTop="17dp"
    android:text="Insert on the field below how much you have in your bank." />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/tvErroSenha1"
    android:layout_below="@+id/tvErroSenha1"
    android:layout_marginTop="41dp"
    android:text="Insert on the field below how much is your credit card limit." />

<EditText
    android:id="@+id/etSaldo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/tvErroSenha1"
    android:ems="10"
    android:inputType="numberDecimal" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/etCartaoLimite"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:ems="10"
    android:inputType="numberDecimal" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/etCartaoLimite"
    android:layout_below="@+id/etCartaoLimite"
    android:text="About your credt card how much you&apos;ve spent already." />

<EditText
    android:id="@+id/etCartao"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView3"
    android:layout_below="@+id/textView3"
    android:ems="10"
    android:inputType="numberDecimal" />

<Button
    android:id="@+id/btRegisterSaldo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/etCartao"
    android:layout_below="@+id/etCartao"
    android:layout_marginLeft="14dp"
    android:text="Register" />


抱歉,伙计们,由于我声望不够,我无法展示图片。 - Cristiano Guerra
6个回答

9
请在AndroidManifest.xml中添加属性android:windowSoftInputMode="adjustResize"
<activity
    ......
    android:windowSoftInputMode="adjustResize">

    ...
</activity>

1
这个属性实际上是做什么的? - Akash Bisariya
3
因为它根本没有帮助理解问题,所以我会给它一个“踩”的评价。仅仅是说“这样做”,这并不应该被赞同,即使它确实有效。 - Mr. Nobody

2

如果您的布局实现了 android:descendantFocusability="blocksDescendants",请检查您的 layout.xml 文件并将其删除。


0
在我的情况下,使用安卓的EditText而不是我自定义的EditText解决了问题。

0

从xml中删除此行:

<requestFocus />

否则在代码中你可以添加这个:

editText.clearFocus();

我应该在所有EditText上添加clearFocus(),还是只在出现问题的那个上面添加? - Cristiano Guerra
无论在哪里出现问题:)您可以添加到所有内容中。我认为在您的xml中,您仅向一个<requestFocus />添加了它,这导致了错误。 - Sushil
是的,我尝试让它工作,阅读了一些教程和其他答案,他们说这将解决我的问题,但它并没有。 - Cristiano Guerra

0

嗨,请检查您的AndroidManifest.xml活动, 并进行一些更改,如下所示

android:windowSoftInputMode="stateAlwaysVisible"

在这个布局示例的活动中:

<activity
    android:name="com.example.a.MainActivity"
    android:windowSoftInputMode="stateAlwaysVisible" // This will make your keyboard work
    android:label="@string/app_name" >

0

你的意思是当你启动活动时,软键盘会弹出吗?如果是这样,你只需要添加以下代码:

android:windowSoftInputMode="adjustResize"

将以下代码添加到您的AndroidManifest.xml文件中,例如:

   <activity
        android:name="com.fortrun.testcrashlytics.MainActivity"
        android:label="@string/app_name"
        android:windowSoftInputMode="adjustResize" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

希望能帮到您!


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