2013-08-18 17 views
54

在您評分並標記爲重複之前,請閱讀我的問題。如何使EditText在創建活動時不重點

我已閱讀討論此問題的其他問題,並且它們都適用於我的佈局,除了第一個創建的

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

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

^,使得它如此至少鍵盤不啓動時彈出,但EditText仍然集中在。

這是XMLEditText

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

這是個什麼樣子,當我打開我的活動,如:

enter image description here

的問題是,有一些電話,當一個EditText是這樣專注的,他們不能寫在裏面。我想要它不是重點。

我所做的工作中提出了在未來的佈局,它的EditTexts不集中,並會看起來更象這樣:

enter image description here

請注意,這是相同的佈局,雖然。這是用戶返回到此屏幕後的屏幕,這表明XML沒有錯,因爲這與XML相同,但問題在於EditText僅在創建活動時關注。

我已經完成了研究,其他所有問題都無法幫助我解決這個問題(但他們確實幫助鍵盤沒有出現,謝天謝地)。我該如何做到這一點,以便啓動時的EditText看起來像第二個截圖,而不是第一個?

+0

所以你要專注於'EditText'? –

+2

你在'EditText'中嘗試過'android:focusable =「false」嗎? –

+0

@PratikButani,使得它永遠不會被聚焦,雖然。 –

回答

246

您可以設置佈局的屬性一樣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> 

願這一個有益的;)

+6

我嘗試了多種解決方案,但只有這一個工程! – MOHRE

+1

很好的答案,謝謝 – Nininea

+2

謝謝你保存我的時間... + 1票 –

-6

使用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> 
+0

它的作品,但它使它永遠不會集中,即使觸摸後。 –

+0

當你需要使它可以聚焦時,你可以使用事件監聽器 –

+0

編輯你的答案,並且我會爲你投票。不過,有人已經回答了你所說的話。 –

15

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和設置通過代碼真正

+0

好的解決方案...答案標記IME選項'actionNext'完全失敗的正確結果。 –

+0

我先試了一下它沒有工作,我的壞測試是在沒有觸摸屏的模擬器上,在真實設備上它的工作就像一個魅力,謝謝@Meher –

0

它可以使用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> 
0

您可以通過編程做到這一點。在您的活動onStart()方法使用editText.setEnabled(false)(未在的onCreate() - 因爲這是用於初始化GUI組件的一些方法)

+0

這將完全禁用EditText,這不是需要的。我不確定(因爲我沒有測試過),但我不認爲禁用,然後重新啓用也可以。 除非有什麼我失蹤,請確保你瞭解這個問題。 –

11

在你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> 
+1

編輯文本未被聚焦,但鍵盤可見。 – viper

1

XML代碼

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

Java代碼onClickListerner

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

這的onCreate()

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

或onCreateView()添加

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

最好的解決辦法是在這裏: https://stackoverflow.com/a/45139132/3172843

正確和簡單解決方案是setFocusable false和setFocusableInTouchMode true。這樣的EditText增益焦點只有當用戶觸摸的是EditText上

android:focusable="false" 
android:focusableInTouchMode="true" 
0
<activity 
      android:name=".MainActivity" 
      android:windowSoftInputMode="stateAlwaysHidden" /> 

機器人:windowSoftInputMode = 「stateAlwaysHidden」

在Manifest.xml文件的活動標籤加入這一行。當你點擊TextEdit時,鍵盤變得焦點。

0

最簡單的方法是添加

android:windowSoftInputMode="stateAlwaysHidden|adjustPan" 

在manifest.xml的活動標籤文件

相關問題