简体   繁体   English

活动启动时隐藏键盘

[英]hide keyboard when activity launch

I have an activity that uses a lot of edittext, and when the activity launches the keyboard is already out. 我有一个使用大量edittext的活动,并且该活动启动时,键盘已经没电了。 I would want that the keyboard is only displayed when the user clicks on an edittext. 我希望仅在用户单击编辑文本时才显示键盘。 My other concern is that the activity starts in the middle, where I would prefer the activity to start from the top. 我的另一个担心是,该活动从中间开始,我希望该活动从顶部开始。

Below is the layout file: 下面是布局文件:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollProfile"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:alpha="0.9"
    android:background="#ffffff" >

<RelativeLayout
    android:id="@+id/main"
    android:layout_gravity="top"
    android:layout_width="match_parent"
    android:layout_height="797dp"

    android:orientation="vertical" >

    <TextView
        android:id="@+id/tASearchDistance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="39dp"
        android:gravity="left"
        android:text="Profile Creation"
        android:textColor="#ffffff"
        android:textSize="28sp"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:id="@+id/tSex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/etxtname"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:text="Upload your Profile Picture"
        android:textColor="#f2f2f2"
        android:textSize="18sp"
        android:textStyle="bold"
        android:typeface="sans" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/etxtheadline"
        android:layout_below="@+id/texperience" >

        <RadioButton
            android:id="@+id/rimale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="Male"
            android:textColor="#000" />

        <RadioButton
            android:id="@+id/rifemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Female"
            android:textColor="#000" />
    </RadioGroup>

    <SeekBar
        android:id="@+id/sbseekBarDistance"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView12"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="11dp"
        android:progress="50" />



    <RadioGroup
        android:id="@+id/radioGroup3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tAge"
        android:layout_below="@+id/tAge" >

        <RadioButton
            android:id="@+id/rlmale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="Male"
            android:textColor="#000" />

        <RadioButton
            android:id="@+id/rlfemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Female"
            android:textColor="#000" />
    </RadioGroup>



    <SeekBar
        android:id="@+id/sbseekBarMinimumAge"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tName4"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="11dp"
        android:progress="25" />

    <TextView
        android:id="@+id/tvseekBarDistanceValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/tvMinAge"
        android:layout_below="@+id/sbseekBarDistance"
        android:text="50"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000"
        android:textSize="18sp"
        android:typeface="serif" />

    <TextView
        android:id="@+id/textView12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/radioGroup1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="19dp"
        android:text="Individual search distance "
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#3D3C3A"
        android:textSize="18sp"
        android:textStyle="bold"
        android:typeface="serif" />

    <SeekBar
        android:id="@+id/sbseekBarMaximumAge"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView14"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="11dp"
        android:progress="50" />

    <TextView
        android:id="@+id/conditions"
        android:layout_width="280dp"
        android:layout_height="130dp"
        android:layout_below="@+id/btnConfirm"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="7dp"
        android:layout_marginTop="7dp"
        android:alpha="0.5"
        android:gravity="center"
        android:text="@string/disclaimer"
        android:textColor="#000"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/tAge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/radioGroup1"
        android:layout_alignRight="@+id/btnConfirm"
        android:text="Looking for"
        android:textColor="#3D3C3A"
        android:textColorHint="#3D3C3A"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/texperience"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/radioGroup1"
        android:layout_below="@+id/etxtheadline"
        android:layout_marginTop="39dp"
        android:text="I am a"
        android:textColor="#3D3C3A"
        android:textSize="18sp"
        android:textStyle="bold" />

    <SeekBar
        android:id="@+id/sbseekBarActivityDistance"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/sbseekBarDistance"
        android:layout_below="@+id/tName01"
        android:progress="50" />

    <Button
        android:id="@+id/btnBack"
        android:layout_width="120dp"
        android:layout_height="60dp"
        android:layout_alignLeft="@+id/radioGroup1"
        android:layout_below="@+id/sbseekBarActivityDistance"
        android:layout_marginTop="49dp"
        android:alpha="0.8"
        android:background="#ccc"
        android:text="Back"
        android:textColor="#000"
        android:textSize="17sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvseekBarActivityDistanceValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvseekBarDistanceValue"
        android:layout_below="@+id/sbseekBarActivityDistance"
        android:text="50"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tName01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvseekBarDistanceValue"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="14dp"
        android:text="Activities search distance"
        android:textSize="18sp"
        android:textStyle="bold"
        android:typeface="serif" />

    <Button
        android:id="@+id/btnPictureSelect"
        android:layout_width="118dp"
        android:layout_height="60dp"
        android:layout_alignBottom="@+id/profilePicturePreview"
        android:layout_alignLeft="@+id/btnFacebookUpload"
        android:layout_alignRight="@+id/btnFacebookUpload"
        android:alpha="0.8"
        android:background="#3D3C3A"
        android:gravity="center_vertical|center_horizontal|left"
        android:onClick="pickPhoto"
        android:text="Upload from device gallery"
        android:textColor="#ffffff"
        android:textSize="15sp"
        android:textStyle="bold"
        android:typeface="serif" />

    <Button
        android:id="@+id/btnFacebookUpload"
        android:layout_width="160dp"
        android:layout_height="55dp"
        android:layout_alignLeft="@+id/textView12"
        android:layout_below="@+id/tvPaid4"
        android:layout_marginTop="5dp"
        android:background="@drawable/facebookiupload"
        android:textColor="#ffffff"
        android:textSize="17sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tName4"
        android:layout_below="@+id/tvMinAge"
        android:layout_marginTop="16dp"
        android:text="Maximum Age Looking For"
        android:textColor="#3D3C3A"
        android:textSize="16sp"
        android:textStyle="bold"
        android:typeface="serif" />

    <EditText
        android:id="@+id/etxtheadline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/etxtage"
        android:layout_alignRight="@+id/conditions"
        android:layout_below="@+id/tvMaxAge"
        android:ems="10"
        android:gravity="left"
        android:hint="Quick description about you"
        android:maxLength="50"
        android:singleLine="true"
        android:textAlignment="gravity"
        android:textColor="#000"
        android:textColorHint="#3D3C3A"
        android:textSize="17sp"
        android:typeface="serif" />

    <Button
        android:id="@+id/btnConfirm"
        android:layout_width="120dp"
        android:layout_height="60dp"
        android:layout_alignBaseline="@+id/btnBack"
        android:layout_alignBottom="@+id/btnBack"
        android:layout_alignRight="@+id/tvPaid4"
        android:alpha="0.8"
        android:background="@drawable/blue_bac2"
        android:text="Confirm"
        android:textColor="#ffffff"
        android:textSize="17sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/profilePicturePreview"
        android:layout_width="132dp"
        android:layout_height="120dp"
        android:layout_alignTop="@+id/btnFacebookUpload"
        android:layout_toRightOf="@+id/btnPictureSelect"
        android:alpha="1"
        android:background="@drawable/border_image"
        android:cropToPadding="true"
        android:padding="3dp"
        android:scaleType="centerCrop" />

    <TextView
        android:id="@+id/tName4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tName01"
        android:layout_below="@+id/etxtage"
        android:layout_marginTop="18dp"
        android:text="Minimum Age Looking For"
        android:textColor="#3D3C3A"
        android:textSize="16sp"
        android:textStyle="bold"
        android:typeface="serif" />

    <EditText
        android:id="@+id/etxtage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btnPictureSelect"
        android:layout_alignRight="@+id/sbseekBarMinimumAge"
        android:layout_below="@+id/btnPictureSelect"
        android:layout_marginTop="22dp"
        android:ems="10"
        android:hint="Please enter your age here"
        android:inputType="number"
        android:maxLength="2"

        android:textAlignment="center"
        android:textColor="#000"
        android:textColorHint="#3D3C3A"
        android:textSize="17sp"
        android:typeface="serif" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/tvMinAge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/sbseekBarMinimumAge"
        android:layout_centerHorizontal="true"
        android:text="25"
        android:textColor="#000"
        android:textSize="18sp"
        android:typeface="serif" />

    <TextView
        android:id="@+id/tvMaxAge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/sbseekBarMaximumAge"
        android:layout_centerHorizontal="true"
        android:text="50"
        android:textColor="#000"
        android:textSize="18sp"
        android:typeface="serif" />

    <TextView
        android:id="@+id/tProfileTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/conditions"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:text="Welcome to Dooba. We provide you with a date for everything you want to do. To begin, please fill out all of the information below."
        android:textSize="16sp"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:id="@+id/tvPaid4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/tSex"
        android:layout_centerHorizontal="true"
        android:text="Upload your profile picture"
        android:textSize="18sp"
        android:textStyle="bold"
        android:typeface="serif" />

    <TextView
        android:id="@+id/tvPaid1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvPaid4"
        android:layout_below="@+id/tProfileTitle"
        android:layout_marginTop="34dp"
        android:text="Profile Creation"
        android:textColor="#ff0000"
        android:textSize="25sp"
        android:textStyle="bold|normal"
        android:typeface="serif" />

    <EditText
        android:id="@+id/etxtname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tProfileTitle"
        android:layout_alignRight="@+id/etxtheadline"
        android:layout_below="@+id/tvPaid1"
        android:layout_marginTop="25dp"
        android:ems="10"
          android:focusableInTouchMode="true"
        android:enabled="true"
        android:gravity="left"
        android:hint="Please type your name here"
        android:inputType="textPersonName"
        android:maxLength="30"
        android:textColor="#000"
        android:textColorHint="#3D3C3A"
        android:textSize="18sp"
        android:typeface="serif" />

</RelativeLayout>

</ScrollView>

Any help would be greatly appreciated. 任何帮助将不胜感激。

Here are two methods use can use to hide keyboard. 这是可以用来隐藏键盘的两种方法。

 public static void hideSystemKeyBoard1(Context mcontext) {
  InputMethodManager imm = (InputMethodManager) mcontext
    .getSystemService(Context.INPUT_METHOD_SERVICE);
   if (imm.isActive())  
   imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
     InputMethodManager.HIDE_NOT_ALWAYS);
 }


 public static void hideSystemKeyBoard2(Context mcontext,View v) {
  InputMethodManager imm = (InputMethodManager) ((AbstractMmtClientActivity) mcontext)
    .getSystemService(Context.INPUT_METHOD_SERVICE);
  imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
 }

Or you can remove <requestFocus /> in all of the EditText 或者,您可以在所有EditText删除<requestFocus />

You can use this code in your manifest. 您可以在清单中使用此代码。 This will hide the Keyboard when the activity starts. 活动开始时,这将隐藏键盘。

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

or you can use this in java code. 或者您可以在Java代码中使用它。

getWindow().setSoftInputMode(
      WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

this will keep the keyboard hidden until and unless you touch edit text. 除非您触摸编辑文本,否则这将使键盘保持隐藏状态。

HOPE this helps :) 希望这可以帮助 :)

    enter code here
//  That Code Write in Manifest 
    <activity android:name="com.package.ClassName" android:configChanges="orientation|screenSize" android:windowSoftInputMode="stateHidden">
            </activity>

//or other Wise  you Use Below Method

/**
     * Method for hiding soft key board

     * @param activity
     */

    public static void hideSoftKeyboard(Activity activity) {
        try {
            if(activity.getCurrentFocus()!=null) {
                InputMethodManager inputMethodManager = (InputMethodManager)activity. getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM