android代码移除焦点,android-如何从单个editText移除焦点
android-如何從單個editText移除焦點
在我的應用程序中,我只有一個button.seFocusableInTouchMode(),以及一些button.requestFocus(),按鈕和一個微調器。 我認為,我的EditText獲得關注,因為它是此活動中唯一可關注的視圖。 我的EditText在字段上顯示帶有橙色邊框和光標。
現在,我想從此字段中刪除焦點(我不希望顯示光標和邊框)。 有沒有辦法做到這一點?
通過執行button.seFocusableInTouchMode()和button.requestFocus(),我能夠專注于按鈕。但這突出了按鈕,顯然不是我想要的。
tobbenb3 asked 2020-01-01T03:23:17Z
16個解決方案
44 votes
您是否嘗試過使用舊商品View.clearFocus()
MishaZ answered 2020-01-01T03:23:46Z
38 votes
android的新手。
getWindow().getDecorView().clearFocus();
這個對我有用..
只需添加..您的布局應具有:
android:focusable="true"
android:focusableInTouchMode="true"
Zaraki answered 2020-01-01T03:24:15Z
22 votes
檢查此問題和選定的答案:阻止EditText專注于Activity啟動。這很丑陋,但是有效,據我所知,沒有更好的解決方案。
Maragues answered 2020-01-01T03:23:26Z
15 votes
我將嘗試通過更多詳細信息和理解來說明如何從EditText視圖中刪除焦點(閃爍的光標)。 通常這行代碼應該可以工作
editText.clearFocus()
但可能是editText仍具有焦點的情況,這是因為clearFocus()方法試圖將焦點設置回活動/片段布局中的第一個可聚焦視圖。
因此,如果您在活動中只有一個可聚焦的視圖,并且通常是您的EditText視圖,則clearFocus()將焦點再次設置為該視圖,并且對您來說,clearFocus()無法正常工作。請記住,默認情況下,EditText視圖是focusable(true),因此,如果布局中只有一個EditText視圖,它將無法在屏幕上獲得焦點。 在這種情況下,您的解決方案將是在布局文件中找到父視圖(某些布局,例如LinearLayout,Framelayout),然后將此xml代碼設置為該父視圖
android:focusable="true"
android:focusableInTouchMode="true"
之后,當您執行editText.clearFocus()時,布局內的父視圖將接受焦點,并且您的editText將清除焦點。
我希望這將有助于某人了解clearFocus()的工作方式。
Sniper answered 2020-01-01T03:24:54Z
3 votes
我知道為時已晚,但對于有同樣需求的人,您正在尋找editText.setFocusable(false)。
lm2a answered 2020-01-01T03:25:15Z
2 votes
使用附帶的代碼將焦點移到“其他人”上,如果您只想關閉鍵盤并釋放焦點,而不必在意誰會得到,則可以這樣做。
像這樣使用:FocusHelper.releaseFocus(viewToReleaseFocusFrom)
public class FocusHelper {
public static void releaseFocus(View view) {
ViewParent parent = view.getParent();
ViewGroup group = null;
View child = null;
while (parent != null) {
if (parent instanceof ViewGroup) {
group = (ViewGroup) parent;
for (int i = 0; i < group.getChildCount(); i++) {
child = group.getChildAt(i);
if(child != view && child.isFocusable())
child.requestFocus();
}
}
parent = parent.getParent();
}
}
}
文件:該方法從子視圖到視圖樹遍歷,并尋找第一個要聚焦的子對象。
編輯:您還可以為此使用API:
View focusableView = v.focusSearch(View.FOCUS_DOWN);
if(focusableView != null) focusableView.requestFocus();
Sveinung Kval Bakken answered 2020-01-01T03:25:48Z
2 votes
我在editText上也遇到了類似的問題,自從活動開始以來,它就獲得了關注。 我很容易解決此問題,如下所示:
您將這段代碼添加到包含xml中editText的布局中:
android:id="@+id/linearlayout"
android:focusableInTouchMode="true"
不要忘記android:id,沒有它我有一個錯誤。
我對editText的另一個問題是,一旦它獲得了第一個焦點,焦點就永遠不會消失。 這是我的Java代碼的一部分,它有一個editText和一個捕獲editText中的文本的按鈕:
editText=(EditText) findViewById(R.id.et1);
tvhome= (TextView)findViewById(R.id.tv_home);
etBtn= (Button) findViewById(R.id.btn_homeadd);
etBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
tvhome.setText( editText.getText().toString() );
//** this code is for hiding the keyboard after pressing the button
View view = Settings.this.getCurrentFocus();
if (view != null)
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
//**
editText.getText().clear();//clears the text
editText.setFocusable(false);//disables the focus of the editText
Log.i("onCreate().Button.onClickListener()", "et.isfocused= "+editText.isFocused());
}
});
editText.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(v.getId() == R.id.et1)
{
v.setFocusableInTouchMode(true);// when the editText is clicked it will gain focus again
//** this code is for enabling the keyboard at the first click on the editText
if(v.isFocused())//the code is optional, because at the second click the keyboard shows by itself
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
}
//**
Log.i("onCreate().EditText.onClickListener()", "et.isfocused= "+v.isFocused());
}
else
Log.i("onCreate().EditText.onClickListener()", "the listener did'nt consume the event");
}
});
希望對您有些幫助!
Lena answered 2020-01-01T03:26:26Z
2 votes
只是找到另一個視圖并給予焦點即可。
var refresher = FindViewById(Resource.Id.refresher);
refresher.RequestFocus();
PmanAce answered 2020-01-01T03:26:46Z
2 votes
如果Edittext父級布局是Linear,則添加
android:focusable="true"
android:focusableInTouchMode="true"
像下面
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true">
............
當Edittext父級布局為相對時,則
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
喜歡
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">
............
OmiK answered 2020-01-01T03:27:19Z
1 votes
我已經做了很多嘗試來清除編輯文本的焦點。 clearfocus()和focusable等東西對我沒有用。因此,我想到了讓假的edittext獲得關注的想法:
...
android:layout_width="match_parent"
android:layout_height="match_parent">
...
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fake"
android:textSize="1sp"/>
然后在您的Java代碼中:
View view = Activity.this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
fake.requestFocus();
}
它會隱藏鍵盤,并移走具有該鍵盤的所有edittext的焦點。 而且正如您所看到的,假的edittext在屏幕外,無法看到
navid answered 2020-01-01T03:27:49Z
0 votes
只要包括這行
android:selectAllOnFocus="false"
在與EditText布局相對應的XML段中。
harshvardhan answered 2020-01-01T03:28:14Z
0 votes
您只需要從視圖中清除焦點即可
EditText.clearFocus()
Mayank Bhatnagar answered 2020-01-01T03:28:34Z
0 votes
如果我正確理解了您的問題,這應該可以幫助您:
TextView tv1 = (TextView) findViewById(R.id.tv1);
tv1 .setFocusable(false);
kPieczonka answered 2020-01-01T03:28:54Z
0 votes
由于我是在小部件中而不是在活動中,所以我做了:
`getRootView()。clearFocus();
kingston answered 2020-01-01T03:29:18Z
0 votes
android:clickable="false"
android:focusable="false"
android:textSize="40dp"
android:textAlignment="center"
android:textStyle="bold"
android:textAppearance="@style/Base.Theme.AppCompat.Light.DarkActionBar"
android:text="AVIATORS"/>
vithika answered 2020-01-01T03:29:34Z
0 votes
您只需要使用屬性設置ViewGroup:
android:focusableInTouchMode="true"
ViewGroup是包含每個子視圖的布局。
GaijinForce answered 2020-01-01T03:30:03Z
總結
以上是生活随笔為你收集整理的android代码移除焦点,android-如何从单个editText移除焦点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 构造函数和实例化原理
- 下一篇: opengl实现三维动画简单代码_使用P