android - unfocus - edittext focusable false
阻止EditText在Activity啟動時獲得關注 (20)
Luc和Mark的優秀答案卻缺少一個好的代碼示例。 將標記android:focusableInTouchMode="true"
到<LinearLayout>
,如下例所示將解決問題。
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:nextFocusUp="@id/autotext"
android:nextFocusLeft="@id/autotext"/>
我在Android中有一個Activity
,有兩個元素:
-
EditText
-
ListView
當我的Activity
啟動時, EditText
立即具有輸入焦點(閃爍光標)。 我不希望任何控件在啟動時具有輸入焦點。 我試過了:
EditText.setSelected(false);
沒運氣。 如何在Activity
開始時說服EditText
不選擇自己?
以下內容將阻止edittext在創建時獲得焦點,但在觸摸時抓住它。
<EditText
android:id="@+id/et_bonus_custom"
android:focusable="false" />
所以你在xml中將focusable設置為false,但關鍵是在java中,你添加了以下監聽器:
etBonus.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.setFocusable(true);
v.setFocusableInTouchMode(true);
return false;
}
});
因為您返回false,即不消耗該事件,所以聚焦行為將像平常一樣進行。
使用其他海報提供的信息,我使用了以下解決方案:
在佈局XML中
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:id="@+id/linearLayout_focus"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- AUTOCOMPLETE -->
<AutoCompleteTextView
android:id="@+id/autocomplete"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:inputType="textNoSuggestions|textVisiblePassword"/>
在onCreate()
private AutoCompleteTextView mAutoCompleteTextView;
private LinearLayout mLinearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
//get references to UI components
mAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout_focus);
}
最後,在onResume()
@Override
protected void onResume() {
super.onResume();
//do not give the editbox focus automatically when activity starts
mAutoCompleteTextView.clearFocus();
mLinearLayout.requestFocus();
}
問題似乎來自於我只能在佈局的XML form
中看到的屬性。
確保在EditText
XML標記內的聲明末尾刪除此行:
<requestFocus />
這應該是這樣的:
<EditText
android:id="@+id/emailField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress">
//<requestFocus /> /* <-- without this line */
</EditText>
在Manifest.xml
文件的activity標記中添加android:windowSoftInputMode="stateAlwaysHidden"
。
在onCreate
方法中添加以下內容:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
在父佈局中寫下這一行......
android:focusableInTouchMode="true"
在第一個可編輯字段之前嘗試此操作:
<TextView
android:id="@+id/dummyfocus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/foo"
/>
----
findViewById(R.id.dummyfocus).setFocusableInTouchMode(true);
findViewById(R.id.dummyfocus).requestFocus();
存在更簡單的解決方案。 在父佈局中設置這些屬性:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
現在,當活動開始時,此主要佈局將默認獲得焦點。
此外,我們可以通過再次將焦點放在主佈局上,在運行時從子視圖中刪除焦點(例如,在完成子編輯之後),如下所示:
findViewById(R.id.mainLayout).requestFocus();
Guillaume Perrot的 好評 :
android:descendantFocusability="beforeDescendants"
似乎是默認值(整數值為0)。 它只是通過添加android:focusableInTouchMode="true"
。
實際上,我們可以看到beforeDescendants
在ViewGroup.initViewGroup()
方法(Android 2.2.2)中設置為默認值。 但不等於0. ViewGroup.FOCUS_BEFORE_DESCENDANTS = 0x20000;
感謝紀堯姆。
實際問題是你根本不希望它有焦點嗎? 或者您不希望它通過聚焦EditText
來顯示虛擬鍵盤? 我沒有看到EditText
關注start的問題,但是當用戶沒有明確請求關注EditText
(並因此打開鍵盤)時,打開softInput窗口肯定是個問題。
如果是虛擬鍵盤的問題,請參閱AndroidManifest.xml
<activity>元素文檔。
android:windowSoftInputMode="stateHidden"
- 在輸入活動時始終隱藏它。
或者android:windowSoftInputMode="stateUnchanged"
- 不要更改它(例如,如果它尚未顯示,則不顯示,但如果在進入活動時打開,則將其保持打開狀態)。
對我來說,在所有設備上運行的是:
<!-- fake first focusable view, to allow stealing the focus to itself when clearing the focus from others -->
<View
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
在有問題的焦點視圖之前把它作為一個視圖,就是這樣。
您可以在layout
的第一個TextView
上將“可聚焦”和“可聚焦在觸摸模式”設置為true。 通過這種方式,當活動開始時, TextView
將被聚焦,但由於其性質,您將看不到任何聚焦在屏幕上,當然,將不會顯示鍵盤...
我做的最簡單的事情是將焦點放在onCreate中的另一個視圖上:
myView.setFocusableInTouchMode(true);
myView.requestFocus();
這使軟鍵盤停止運行,EditText中沒有光標閃爍。
我曾單獨嘗試了幾個答案,但重點仍然是EditText。 我只是通過使用以下兩個解決方案來設法解決它。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
(來自Silver的參考https://.com/a/8639921/15695 )
並刪除
<requestFocus />
在EditText
(參考文獻floydaddict share )
我需要以編程方式清除所有字段的焦點。 我剛剛將以下兩個語句添加到主佈局定義中。
myLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
myLayout.setFocusableInTouchMode(true);
而已。 馬上解決了我的問題。 謝謝,西爾弗,指出我正確的方向。
是的我做了同樣的事情 - 創建一個'虛擬'線性佈局,獲得初始焦點。 此外,我設置了“下一個”焦點ID,因此用戶在滾動一次後無法再對焦:
<LinearLayout 'dummy'>
<EditText et>
dummy.setNextFocusDownId(et.getId());
dummy.setNextFocusUpId(et.getId());
et.setNextFocusUpId(et.getId());
很多工作只是為了擺脫對視圖的關注..
謝謝
最簡單的答案,只需在XML的父佈局中添加它。
android:focusable="true"
android:focusableInTouchMode="true"
由於我不喜歡用與功能相關的東西污染XML,我創建了這個方法,“透明地”從第一個可聚焦視圖中竊取焦點,然後確保在必要時自行移除!
public static View preventInitialFocus(final Activity activity)
{
final ViewGroup content = (ViewGroup)activity.findViewById(android.R.id.content);
final View root = content.getChildAt(0);
if (root == null) return null;
final View focusDummy = new View(activity);
final View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View view, boolean b)
{
view.setOnFocusChangeListener(null);
content.removeView(focusDummy);
}
};
focusDummy.setFocusable(true);
focusDummy.setFocusableInTouchMode(true);
content.addView(focusDummy, 0, new LinearLayout.LayoutParams(0, 0));
if (root instanceof ViewGroup)
{
final ViewGroup _root = (ViewGroup)root;
for (int i = 1, children = _root.getChildCount(); i < children; i++)
{
final View child = _root.getChildAt(i);
if (child.isFocusable() || child.isFocusableInTouchMode())
{
child.setOnFocusChangeListener(onFocusChangeListener);
break;
}
}
}
else if (root.isFocusable() || root.isFocusableInTouchMode())
root.setOnFocusChangeListener(onFocusChangeListener);
return focusDummy;
}
這些解決方案都不適合我。 我修復自動對焦的方式是:
<activity android:name=".android.InviteFriendsActivity" android:windowSoftInputMode="adjustPan">
<intent-filter >
</intent-filter>
</activity>
這是最完美,最簡單的解決方案。我總是在我的應用程序中使用它。
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);