防止EditText自动获取焦点

3

我有一个Android活动,整个布局中只有一个 EditText 。出于某种原因,每当活动开始时,键盘就会弹出。 我已经尝试了所有以下事项:

  • Placing these two in OnStart:

    FindViewById<EditText> (Resource.Id.searchText).ClearFocus ();
    FindViewById<EditText> (Resource.Id.searchText).Selected = false;
    
  • Adding a LinearLayout with these two properties:

    android:focusable="true" android:focusableInTouchMode="true"
    
  • Adding the following tag inside another View on the layout:

    android:focusable="true" android:focusableInTouchMode="true"
    
  • Adding this to my activity manifest:

    android:windowSoftInputMode="stateHidden"
    

但是,当活动打开时,键盘仍然会弹出。我可能做错了什么?


如果有人正在查看此内容并考虑将其标记为重复,请不要这样做。我已经阅读了这个问题,并尝试了所有方法。 - James Monger
5个回答

7
尝试使用这些内容来进行跨平台的Xamarin.Android开发:Xamarin.Android
this.Window.SetSoftInputMode (SoftInput.StateHidden);

或者

将以下内容添加到清单文件中,

[Activity(Label = "SampleApp", MainLauncher = true, Icon = "@drawable/icon", WindowSoftInputMode = SoftInput.StateHidden)]

谢谢,你救了我们的一天!没有黑客攻击,没有额外代码,纯粹的完美。 <3 - CodeMylife

4
尝试这个代码 - this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);。它可以隐藏输入法。

2
将以下内容添加到清单文件中...
<activity android:name=".YourActivity" android:windowSoftInputMode="stateHidden"  />

0

我知道这个很古老,但是我发现将它添加到 XML 文件中的根视图通常有助于防止这个问题:

 android:descendantFocusability="beforeDescendants"
 android:focusableInTouchMode="true"

0

Android默认会将光标定位在第一个可聚焦的对象上。虽然kirankk的回答适用于标准活动,但如果您正在使用具有自定义视图的DialogFragment,则更简单的选择(顺便提一下,这也适用于标准活动)是使包含文本视图的ViewGroup(即LinearLayout/RelativeLayout)可聚焦且触摸模式下可聚焦。这可以从AXML中完成,并防止TextView成为第一个可聚焦的视图。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接