How to make EditText not focused when creating Act

2019-01-10 09:21发布

Before you downvote and mark as duplicate, read my question.

I've read the other questions discussing this, and all of them work for my layouts, except for the very first one created.

At the moment, this is at the top of my onCreate method:

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

^ That makes it so at least the keyboard doesn't pop up on startup, but the EditText is still focused on.

This is the XML for my EditText:

<EditText
    android:id="@+id/password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/changePass"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="167dp"
    android:ems="10"
    android:imeOptions="flagNoExtractUi"
    android:inputType="textPassword"
    android:maxLength="30" >
</EditText>

This is what it looks like when I bring up my activity:

enter image description here

The problem is that with some phones, when an EditText is focused like this, they can't write in it. I want it to not focus.

What I've done works for the next layouts brought up in that it the EditTexts are not focused on and would look more like this:

enter image description here

Notice that it's the same layout, though. This is the screen after the user has been brought back to this screen, which would indicate nothing wrong with the XML because this is the same XML, but the problem is that the EditText is only focused on when the activity is created.

I've done research and all of the other questions don't help me with this (they did however help the keyboard not show up, thankfully). How can I make it so the EditText on startup will look like the second screenshot, rather than the first?

13条回答
家丑人穷心不美
2楼-- · 2019-01-10 09:46

Add this in onCreate()

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

or in onCreateView()

 getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
查看更多
一纸荒年 Trace。
3楼-- · 2019-01-10 09:56
<activity
            android:name=".MainActivity"
            android:windowSoftInputMode="stateAlwaysHidden" />

android:windowSoftInputMode="stateAlwaysHidden"

add this line in the activity tag of the Manifest.xml file.when you click on the TextEdit the keyboard gets into focus.

查看更多
Fickle 薄情
4楼-- · 2019-01-10 09:56

In Manifest , copy and paste the beneath code.

 <activity
   android:name=".LoginActivity"
   android:windowSoftInputMode="stateAlwaysHidden"/>
查看更多
【Aperson】
5楼-- · 2019-01-10 10:00

use android:focusable="false" for focusing disable

<EditText
    android:id="@+id/password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/changePass"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="167dp"
    android:ems="10"
    android:imeOptions="flagNoExtractUi"
    android:inputType="textPassword"
    android:maxLength="30"
 android:focusable="false" >

</EditText>
查看更多
叛逆
6楼-- · 2019-01-10 10:03

You can set property of Layout like android:descendantFocusability="beforeDescendants" and android:focusableInTouchMode="true"

Example:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/mainLayout"
  android:descendantFocusability="beforeDescendants"
  android:focusableInTouchMode="true" >

    <EditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/changePass"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="167dp"
        android:ems="10"
        android:imeOptions="flagNoExtractUi"
        android:inputType="textPassword"
        android:maxLength="30" >
    </EditText>

</RelativeLayout>

May this one helpful ;)

查看更多
何必那么认真
7楼-- · 2019-01-10 10:05

XML Code

<EditText 
    android:imeOptions="flagNoExtractUi"
    android:focusable="false"
    />

Java Code in onClickListerner

 mEdtEnterPhoneNumber.setFocusable(true);
    mEdtEnterPhoneNumber.setFocusableInTouchMode(true);
查看更多
登录 后发表回答