簡體   English   中英

創建Activity時如何使EditText不聚焦

[英]How to make EditText not focused when creating Activity

我已經閱讀了討論這個問題的其他問題,除了第一個創建的布局之外,所有這些問題都適用於我的布局。

目前,這是我的onCreate方法的頂部:

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

^ 這使得至少鍵盤不會在啟動時彈出,但EditText仍然專注於。

這是我的EditTextXML

<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>

這是我提出活動時的樣子:

在此處輸入圖像描述

問題在於,對於某些手機,當EditText像這樣聚焦時,它們無法在其中寫入。 我希望它集中。

我所做的工作適用於下一個布局,因為EditTexts沒有關注它,並且看起來更像這樣:

在此處輸入圖像描述

請注意,它是相同的布局。 這是用戶返回此屏幕后的屏幕,這表明XML沒有任何問題,因為這是相同的XML ,但問題是EditText僅在創建活動時才關注。

我已經完成了研究,所有其他問題都沒有幫助我解決這個問題(但是他們確實幫助鍵盤沒有出現,謝天謝地)。 我怎樣才能使啟動時的EditText看起來像第二個屏幕截圖,而不是第一個屏幕截圖?

您可以設置布局的屬性,如android:descendantFocusability="beforeDescendants"android:focusableInTouchMode="true"

例子:

<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>

願這有幫助;)

XML 代碼:

<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代碼:

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;
        }
    });

在 xml 中設置可聚焦的 false 並通過代碼將其設置為 true

在你的 main_layout 添加這 2 行:

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

例子:

<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>

最好的解決方案在這里: https ://stackoverflow.com/a/45139132/3172843

正確而簡單的解決方案是 setFocusable false 和setFocusableInTouchMode true 所以只有當用戶觸摸 EditText 時 EditText 才會獲得焦點

android:focusable="false"
android:focusableInTouchMode="true"

在 onCreate() 中添加這個

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

或在 onCreateView()

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

XML 代碼

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

onClickListerner 中的 Java 代碼

 mEdtEnterPhoneNumber.setFocusable(true);
    mEdtEnterPhoneNumber.setFocusableInTouchMode(true);

每當我在 Spinner 的選擇項之后嘗試打開 Editext 時,所以當時無法打開鍵盤並寫下 Edit 文本中的值,但我用這段代碼解決了我的問題。

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

<androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:descendantFocusability="beforeDescendants"
                android:focusableInTouchMode="true">
    
                <androidx.appcompat.widget.AppCompatEditText
                    android:id="@+id/titleName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:inputType="textCapWords"
                    android:hint="@string/hint_your_full_name"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>

謝謝!

可以使用android:focusable="false"來禁用它,但如果由於某種原因這不起作用,那么您可以簡單地放置一個LinearLayout ,它會在不破壞您的布局的情況下獲得焦點。

注意:Eclipse 會給你一個錯誤,說你的LinearLayout是無用的,因為它沒有內容。 您應該可以毫無問題地忽略它。

例如:

<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>

您可以以編程方式執行此操作。 在您的活動的onStart()方法中使用editText.setEnabled(false) (而不是在 onCreate() - 因為這是一些用於初始化 GUI 組件的方法)

<activity
            android:name=".MainActivity"
            android:windowSoftInputMode="stateAlwaysHidden" />

android:windowSoftInputMode="stateAlwaysHidden"

將此行添加到 Manifest.xml 文件的活動標記中。當您單擊 EditText 時,鍵盤將成為焦點。

最簡單的方法是添加

android:windowSoftInputMode="stateAlwaysHidden|adjustPan" 

在 Manifest.xml 文件的活動標記中

1)它最簡單,打開清單並將以下代碼放在活動標簽之間:

android:windowSoftInputMode="stateHidden"

2)將此屬性放在父布局中

android:focusable="true" 
android:focusableInTouchMode="true"

在 Manifest 中,復制並粘貼下面的代碼。

 <activity
   android:name=".LoginActivity"
   android:windowSoftInputMode="stateAlwaysHidden"/>

使用android:focusable="false"禁用對焦

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM