如何在应用程序启动或删除焦点时隐藏键盘?

时间:2015-08-18 05:46:18

标签: android

enter image description here

您好 当我运行该应用程序时,我做了一个简单的应用程序我的键盘自动打开我想从编辑字段中删除焦点或在应用程序启动时第一次隐藏键盘

这是我的代码

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

    <include
        android:id="@+id/top_header"
        layout="@layout/header_second_screen"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" />


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/footer_item"
        android:layout_below="@+id/top_header"
        android:background="#ee3333">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="20dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="UserName"
                    android:textColor="#ffffff"
                    android:textSize="20sp" />

                <EditText
                    android:id="@+id/entry"
                    android:layout_width="0dp"
                    android:layout_height="40dp"
                    android:layout_marginLeft="30dp"
                    android:layout_weight="1"
                    android:background="@drawable/border"
                    android:hint="Add Name"
                    android:padding="5dp"
                    android:singleLine="true"
                    android:textColor="#ffffff" />
            </LinearLayout>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#567234"
                android:text="Login"
                android:layout_gravity="center"/>

        </LinearLayout>

    </ScrollView>


    <include
        android:id="@+id/footer_item"
        layout="@layout/footer_second_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />


</RelativeLayout>

4 个答案:

答案 0 :(得分:3)

一个更简单的技巧。

只需将以下XML属性添加到根RelativeView即可。实际上,您需要确保将要添加这些属性的视图放在之前第一个EditText

<RelativeLayout
...
android:focusable="true"
android:focusableInTouchMode="true"
...
> ... </RelativeLayout>

请告诉我这是否适合您。

答案 1 :(得分:3)

您可以在活动启动时隐藏键盘。

在AndroidManifest.xml中:

<activity android:name="com.your.package.ActivityName"
      android:windowSoftInputMode="stateHidden"  />

或尝试

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)‌​;

我希望它有所帮助!

答案 2 :(得分:0)

You can achieve this from any of these solutions:

Use any of these methods:

/**
* Hides the soft keyboard
*/
public void hideSoftKeyboard() {
           if(getCurrentFocus()!=null) {
                    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                    inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                }
            }

/**
* Shows the soft keyboard
*/
public void showSoftKeyboard(View view) {
      InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
      view.requestFocus();
      inputMethodManager.showSoftInput(view, 0);
}


Or in manifest, you can do this:

<activity android:name="com.your.package.ActivityName"
          android:windowSoftInputMode="stateHidden"  />


Or try this in onCreate() of your activity
                        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)‌​;

Or try this: 

http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft

答案 3 :(得分:0)

用此隐藏键盘

public static void hideKeyboard( Activity activity ) {
        InputMethodManager imm = (InputMethodManager)activity.getSystemService( Context.INPUT_METHOD_SERVICE );
        View f = activity.getCurrentFocus();
        if( null != f && null != f.getWindowToken() && EditText.class.isAssignableFrom( f.getClass() ) )
            imm.hideSoftInputFromWindow( f.getWindowToken(), 0 );
        else 
            activity.getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN );
    }