我想在EditText
聚焦时自动显示软键盘(如果设备没有物理键盘),我有两个问题:
当我Activity
显示时,我EditText
的注意力集中但键盘没有显示,我需要再次点击它来显示键盘(显示我的键盘时应Activity
显示).
当我在键盘上单击完成时,键盘被解除但是EditText
保持聚焦并且不想要(因为我的编辑完成了).
要恢复,我的问题是在iPhone上有更多类似的东西:它使键盘与我的EditText
状态同步(聚焦/不聚焦),当然如果有物理键盘,则不会出现软键盘.
keyboard android focus android-softkeyboard android-edittext
我希望能够从EditText中删除焦点.例如,如果键盘出现,并且用户使用后退按钮隐藏它,我希望焦点和光标消失.怎么做到呢?
我有一个EditText
和一个Button
.点击按钮我想打开EditText
键盘并同时请求重点EditText
,以便用户直接开始键入键盘和文本出现在EditText
.但在我的情况下,当我单击按钮时,它会打开键盘,但它不会将焦点设置在EditText
,因为用户必须EditText
再次单击它才能在其上书写.问题是什么 任何帮助或建议.
代码单击按钮
m_SearchEditText.requestFocus();
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(m_SearchEditText.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
Run Code Online (Sandbox Code Playgroud) 我读到了关于如何设置一个对象的问题,但我似乎无法找到我想要做的答案.
使用On Focus Listener我已完成以下操作:
Ehour.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View arg0, boolean arg1) {
// TODO Auto-generated method stub
if (!arg1) {
if (Ehour.getText().length()<=0) {
Toast.makeText(getApplicationContext(), "No Hour Entered", Toast.LENGTH_LONG).show();
Calendar nohour = Calendar.getInstance();
Ehour.setText(numberformatter(nohour.get(Calendar.HOUR_OF_DAY)));
Ehour.setFocusable(true);
Ehour.requestFocus();
}
}
}});
Run Code Online (Sandbox Code Playgroud)
我试过以下帖子的建议:
我阅读这篇文章,但使用这些建议似乎也不起作用: 如何在创建和显示布局时将焦点设置在视图上?
我的目标是当编辑文本焦点丢失时,如果给出的条目无效,我需要返回到它.
此外,根据比尔莫特的建议,我也尝试过:
Ehour.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View arg0, boolean arg1) {
// TODO Auto-generated method stub
if (!arg1) {
if (Ehour.getText().length()<=0) {
Toast.makeText(getApplicationContext(), "No Hour Entered", Toast.LENGTH_LONG).show();
Calendar nohour = Calendar.getInstance();
Ehour.setText(numberformatter(nohour.get(Calendar.HOUR_OF_DAY)));
((EditText)arg0).requestFocus();
} …
Run Code Online (Sandbox Code Playgroud)