如何隐藏键盘上的回车键 [英] How to hide keyboard on enter key

查看:134
本文介绍了如何隐藏键盘上的回车键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有四个的EditText的活动,我想隐藏键盘,当用户完成使用四个的EditText之一。如果我点击键盘输入,将重点另一个的EditText和键盘依然存在,但我需要隐藏键盘,所以只使用一个EditText上并不见得其他。 这是XML:

i have an activity with four edittext and i want to hide keyboard when users finish to use one of the four edittext. if i click enter on keyboard, it will focus another edittext and keyboard still remain, but i need to hide keyboard, so to use only one edittext and not necessarily the other. This is 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>

你能帮助我吗?

推荐答案

我不知道,但你应该试试这个code: -

I am not sure but you should try this code:-

youredittext.setOnEditorActionListener(new 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(edittext.getWindowToken(), 0);
           return true;
        }
        return false;
    }
});

我希望这帮助..

I hope this help..

这篇关于如何隐藏键盘上的回车键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆