2010-10-08 128 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的焦点它。也如你所看到的假编辑文字是在屏幕之外,无法看到