Android Activity 启动时显示软键盘
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2466516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show soft keyboard when Activity starts
提问by Al.
I have 2 activities, A and B. When A starts, it checks for a condition and if true, it calls startActivityForResult()
to start B. B only takes text input so it makes sense for the soft keyboard to automatically pop up when B start. When the activity starts, the EditText already has focus and it ready for input.
我有 2 个活动,A 和 B。当 A 启动时,它检查条件,如果为真,它调用startActivityForResult()
启动 B。B 只接受文本输入,因此当 B 启动时软键盘自动弹出是有意义的。当活动开始时,EditText 已经获得焦点并准备好输入。
The problem is that the keyboard never shows up, even with windowSoftInputMode="stateAlwaysVisible"
set in the manifest under the <activity>
tag for B. I also tried with the value set to stateVisible
. Since it doesn't show up automatically, I have to tap the EditText to make it show.
问题是键盘永远不会出现,即使windowSoftInputMode="stateAlwaysVisible"
在<activity>
B 标签下的清单中设置。我也尝试将值设置为stateVisible
。由于它不会自动显示,我必须点击 EditText 使其显示。
Anyone know what the solution might be?
任何人都知道解决方案可能是什么?
回答by Leo
What worked best for me is in Android Manifest for activity B adding
最适合我的是在 Android Manifest 中添加活动 B
android:windowSoftInputMode="stateVisible"
android:windowSoftInputMode="stateVisible"
Hope that helps for you as well.
希望对你也有帮助。
回答by Uncaught Exception
Easiest solution: Put
最简单的解决方案:放置
android:windowSoftInputMode = "stateVisible"
in Activity section of AndroidManifest.xml
在 AndroidManifest.xml 的 Activity 部分
回答by synic
回答by Paul
For me worked only this solutions: add in manifest for that activity:
对我来说,只有这个解决方案:为该活动添加清单:
android:windowSoftInputMode="stateVisible|adjustPan"
回答by abc
I have got two way.
我有两种方法。
Method 1.Use the following code inside the OnCreate method
方法一、在OnCreate方法中使用如下代码
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
It will prevent popping up keyboard unless you click.
除非您单击,否则它将防止弹出键盘。
or
或者
Method 2You can move away the focus on other view like TextView by using "requestfocus" in the xml.
方法 2您可以通过在 xml 中使用“requestfocus”将焦点移到 TextView 等其他视图上。
<TextView
android:id="@+id/year_birth_day"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1991">
<requestFocus />
</TextView>
Method 3( I think it should be avoidable) Using the following code in the manifest-
方法3(我认为应该可以避免)在清单中使用以下代码-
android:windowSoftInputMode="stateVisible"
回答by whlk
Try showing the keyboard with some delay. Something similar to this:
尝试延迟显示键盘。类似的东西:
public void onResume() {
super.onResume();
TimerTask tt = new TimerTask() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourTextBox, InputMethodManager.SHOW_IMPLICIT);
}
};
final Timer timer = new Timer();
timer.schedule(tt, 200);
}
回答by Chad Hedgcock
If you're using an emulator, you have to turn the hard keyboard off in order for the soft keyboard to show.
如果您使用的是模拟器,则必须关闭硬键盘才能显示软键盘。
回答by Keyur Sureliya
File : AndroidManifest.xml
文件:AndroidManifest.xml
<activity android:name=".MainActivity">
Add following property :
添加以下属性:
android:windowSoftInputMode="stateVisible"
Which worked for me.
这对我有用。
回答by Ashwini
paste this after setContentView
在 setContentView 之后粘贴这个
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
回答by Kishan Solanki
Major Attention Required!
需要重点关注!
android:windowSoftInputMode="stateVisible|adjustPan"
This alone won't work to show keyboard on activity start.
android:windowSoftInputMode="stateVisible|adjustPan"
仅此一项无法在活动开始时显示键盘。
You also need to explicitly add this into your class
您还需要将其显式添加到您的班级中
editTextXYZ.requestFocus()
val imm: InputMethodManager =
getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(editTextXYZ, InputMethodManager.SHOW_IMPLICIT)