如何在按下回车键时隐藏键盘

10

我有一个带有四个EditText的活动,并且我希望在用户完成使用其中一个EditText后隐藏键盘。如果我在键盘上点击“Enter”键,它将聚焦到另一个EditText上并保留键盘,但我需要隐藏键盘,以便仅使用一个EditText而不一定是其他EditText。

这是XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:background="@drawable/sfondo">
<LinearLayout android:id="@+id/linearLayout1"
    android:layout_width="match_parent" android:layout_height="wrap_content">
    <ImageButton android:id="@+id/backPersonalizza"
        android:src="@drawable/back" android:background="@null"
        android:layout_marginTop="10dip" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_marginLeft="5dp"></ImageButton>
    <TextView android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="@android:color/black" android:id="@+id/testoSuggerimenti"
        android:text="Personalizza il testo e le icone. Clicca su una delle icone e scegli l'immagine preferita"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginRight="5dp"
        ></TextView>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout2"
    android:layout_height="wrap_content" android:layout_width="match_parent"
    android:orientation="vertical" android:layout_marginTop="20dp">
    <LinearLayout android:id="@+id/linearLayout3"
        android:layout_height="wrap_content" android:layout_width="match_parent">
        <EditText android:layout_height="40dp" android:id="@+id/editT1"
            android:layout_width="115dp" android:layout_marginLeft="32dp"
            android:layout_marginRight="25dp" android:gravity="center"
            android:ellipsize="end" android:singleLine="true">
            <requestFocus></requestFocus>
        </EditText>
        <EditText android:layout_height="40dp" android:id="@+id/editT2"
            android:layout_width="115dp" android:layout_marginRight="10dp"
            android:gravity="center" android:ellipsize="end"
            android:layout_marginLeft="10dp" android:singleLine="true"></EditText>
    </LinearLayout>
    <LinearLayout android:layout_weight="1" android:id="@+id/linearLayout4"
        android:layout_height="match_parent" android:layout_width="wrap_content">
        <ImageButton android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:src="@drawable/tasto1"
            android:id="@+id/tastoMod1" android:background="@null"
            android:layout_marginLeft="30dp" android:layout_marginTop="-5dp"></ImageButton>
        <ImageButton android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:src="@drawable/tasto2"
            android:id="@+id/tastoMod2" android:background="@null"
            android:layout_marginLeft="30dp" android:layout_marginTop="-5dp"></ImageButton>
    </LinearLayout>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout5"
    android:layout_height="wrap_content" android:layout_width="match_parent"
    android:orientation="vertical">
    <LinearLayout android:id="@+id/linearLayout6"
        android:layout_height="wrap_content" android:layout_width="match_parent">
        <EditText android:layout_height="40dp" android:id="@+id/editT3"
            android:layout_width="115dp" android:layout_marginLeft="32dp"
            android:layout_marginRight="25dp" android:gravity="center"
            android:ellipsize="end" android:singleLine="true"></EditText>
        <EditText android:layout_height="40dp" android:id="@+id/editT4"
            android:layout_width="115dp" android:layout_marginRight="30dp"
            android:gravity="center" android:ellipsize="end" android:singleLine="true"
            android:layout_marginLeft="10dp"></EditText>
    </LinearLayout>
    <LinearLayout android:id="@+id/linearLayout7"
        android:layout_height="wrap_content" android:layout_width="match_parent">
        <ImageButton android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:src="@drawable/tasto3"
            android:id="@+id/tastoMod3" android:background="@null"
            android:layout_marginLeft="30dp" android:layout_marginTop="-5dp"></ImageButton>
        <ImageButton android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:src="@drawable/tasto4"
            android:id="@+id/tastoMod4" android:background="@null"
            android:layout_marginLeft="30dp" android:layout_marginTop="-5dp"></ImageButton>
    </LinearLayout>
</LinearLayout>

你能帮我吗?

2个回答

26

我不确定,但您应该尝试这段代码:

youredittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {

    @Override
    public boolean onEditorAction(TextView v, int keyCode, KeyEvent event) {
        if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
            // hide virtual keyboard
            InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(youredittext.getWindowToken(), 0);
            return true;
        }
        return false;
    }
});

我希望这个能帮到你。


我会尝试!但是这样只会覆盖回车键,而不是其他按键对吧? - Jayyrus
我编辑了我的回答,我认为尝试将OnEditorActionListener附加到它们所有。 - Uttam
5
对我来说很奇怪,按下回车键但keyCode为0。通过使用event.getKeyCode()方法解决了该问题。 - Seraphim's

19

最好的方法是在XML文件中添加以下代码

android:imeOptions="actionDone"

也许是因为编辑文本是通过程序生成的,没有可编辑的XML文件... - clavio
2
不行!先读问题...他说他有4个editText,他想在任何一个editText完成输入后隐藏键盘。 - devROYAL
因为如果你在EditText上设置TextView.OnEditorActionListener,键盘在按下回车按钮时不会隐藏。如果有更好的选项,那么更差的选项将被弃用:) - the korovay

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