2010-10-08 129 views
47

可能重複:一個單一的EditText連同一些TextViews,按鈕和微調
Stop EditText from gaining focus at Activity startup?如何從單一EDITTEXT移除焦點

在我的應用我有。我相信,我的EditText因爲它是這個活動中唯一可以關注的視圖而受到關注。我的EditText字段上顯示橙色邊框和光標。
現在我想從這個字段中刪除焦點(我不想顯示光標和邊框)。有沒有辦法做到這一點?
我已經能夠通過做button.seFocusableInTouchMode()和button.requestFocus()來關注按鈕。但是這突出了按鈕,顯然不是我想要的。

+8

實際上並不重複。這個問題是關於防止EditText獲得關注活動啓動的問題。這一個是關於消除焦點。這些是不同的問題。 – 2014-07-12 18:19:21

回答

23

檢查這個問題和選定的答案:Stop EditText from gaining focus at Activity startup這是醜陋的,但它的工作原理,據我所知沒有更好的解決方案。

+4

謝謝,我沒有看到那一個。我的EditText被包裹在一個TableLayout,所以這樣做: <的TableRow 機器人:可聚焦= 「真」 機器人:focusableInTouchMode = 「真」> 解決了這個問題對我來說。 – tobbenb3 2010-10-08 13:27:47

+0

太棒了,它確實是一個醜陋的問題,而且解決方案根本不是直觀的。 – Maragues 2010-10-11 08:23:58

+1

和那些不知名的Android API一樣,我在2013年寫了這個。 – sebrock 2013-01-08 14:06:17

41

你有沒有嘗試使用老好View.clearFocus()

+2

對我不起作用。在onCreate中調用它,但EditText保持邊框和閃爍的光標。 – Ixx 2012-05-04 10:38:15

+0

同樣不適用於我 – 2012-09-03 12:04:55

+1

我使用了該方法並添加了OnFocusChangeListener,並且清除了焦點,然後再次設置(自動)。 – 2012-09-04 09:03:40

2

使用附帶的代碼將焦點放到「別人」,這是OK,如果你有一個觀點,你只是想取消鍵盤和釋放焦點,你並不在乎誰是誰。

使用這樣的: 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(); 
25

新到Android ..試過

getWindow().getDecorView().clearFocus(); 

工作對我來說..親切糾正我,如果有什麼不對:)

只是增加..你佈局應該有:

android:focusable="true" 
android:focusableInTouchMode="true" 
+0

它適合我! – GFPF 2015-05-13 17:48:25

3

我知道已經太晚了,但對於某些需要同樣需求的人來說,editText.setFocusable(false)si就是你要找的東西。

0

只需包括在對應於所述的EditText佈局的XML段這條線

android:selectAllOnFocus="false" 

2

我有一個類似的問題,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"); 
     } 
    }); 

希望這將有助於一些你!

0

如果我正確理解你的問題,這會幫助你:

TextView tv1 = (TextView) findViewById(R.id.tv1); 
tv1 .setFocusable(false); 
0

你只需要清除該視圖的焦點,

EditText.clearFocus() 
0

因爲我是一個小部件,而不是在我做了一個活動:

`getRootView()。clearFocus();

2

只需找到另一個視圖,然後重點關注它。

var refresher = FindViewById<MvxSwipeRefreshLayout>(Resource.Id.refresher); 

refresher.RequestFocus(); 
+0

出於某種原因,這對我來說是最好的解決方案。我只是要求關注線性佈局,以便要求編輯文本失去焦點 – 2017-04-30 19:37:32

2

我將試圖解釋如何從EditText上查看一些更多的細節和理解清除病竈(閃爍的光標)。通常,這行代碼應該工作

editText.clearFocus() 

但它可能是情況時EDITTEXT仍然有焦點,而這種情況正在發生,因爲clearFocus()方法試圖將焦點設置回第一可聚焦的觀點在活動/片段佈局中。因此,如果在可聚焦的活動中只有一個視圖,並且這通常是您的EditText視圖,那麼clearFocus()將再次將焦點設置爲該視圖,並且對於您而言,它會查看clearFocus()不管用。 請記住,默認情況下,EditText視圖是可以聚焦的(true),所以如果在佈局中只有一個EditText視圖,它將始終將焦點放在屏幕上。在這種情況下,您的解決方案將是找到父視圖(一些佈局,EX的LinearLayout,FrameLayout裏),你的佈局文件中,並設置它這個XML代碼

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

後,當你執行editText.clearFocus()的您的佈局中的父視圖將接受焦點,並且您的editText將清除焦點。

我希望這可以幫助有人瞭解clearFocus()如何工作。

1

我已經嘗試了很多來清除編輯文本的焦點。 clearfocus()和可聚焦的和其他的東西從來沒有爲我工作。 於是我想出了讓假的EditText獲取焦點的想法:

<LinearLayout 
    ... 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     ... 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

    <!--here comes your stuff--> 

    </LinearLayout> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/fake" 
     android:textSize="1sp"/> 

</LinearLayout> 

然後在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的焦點它。也如你所看到的假編輯文字是在屏幕之外,無法看到