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 10:06

The easiest way is to add

android:windowSoftInputMode="stateAlwaysHidden|adjustPan" 

in the activity tag of the Manifest.xml file

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-10 10:07

XML code:

<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:focusable="false"
    android:imeOptions="flagNoExtractUi"
    android:inputType="textPassword"
    android:maxLength="30" >
</EditText>

Java code:

EditText edPwd = (EditText)findViewById(R.id.password);
edtPwd.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            v.setFocusable(true);
            v.setFocusableInTouchMode(true);
            return false;
        }
    });

set focusable false in xml and set it true via the code

查看更多
冷血范
4楼-- · 2019-01-10 10:08

the best solution is here: https://stackoverflow.com/a/45139132/3172843

the correct and simple solution is to setFocusable false and setFocusableInTouchMode true . so the EditText gain focus only when user touch that EditText

android:focusable="false"
android:focusableInTouchMode="true"
查看更多
混吃等死
5楼-- · 2019-01-10 10:08

It is possible to use android:focusable="false" to disable it, but if that does not work for some reason, then you can simply put a LinearLayout and it will take the focus without disrupting your layout.

NOTE: Eclipse will give you an error saying that your LinearLayout is useless because it has no contents. You should be able to disregard it with no problems.

For example:

<LinearLayout
    android:focusable="true"
    android:focusableInTouchMode="false"
    android:layout_width="0dp"
    android:layout_height="0dp"
    />
<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>
</LinearLayout>
查看更多
在下西门庆
6楼-- · 2019-01-10 10:09

In your main_layout add this 2 lines:

android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"

example:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"> *YOUR LAYOUT CODE* </RelativeLayout>
查看更多
【Aperson】
7楼-- · 2019-01-10 10:11

1) Its the simplest,open the manifest and put the following code between the activity tag:

android:windowSoftInputMode="stateHidden"

2) Put this attributes in the parent layout

android:focusable="true" 
android:focusableInTouchMode="true"
查看更多
登录 后发表回答