问题描述
我在屏幕键盘上遇到麻烦。我有一个活动,一个 EditText
显示键盘,还有一个按钮进入第二个活动。第二个活动在其 onCreate()
上显示一个 ProgressDialog
,执行内容,并关闭 ProgressDialog
。问题是,当显示 ProgressDialog
时,键盘也会显示。 我希望键盘消失在创建 ProgressDialog
之前。我搜索了两个StackOverflow和其他网站,但是似乎没有任何效果。
我附上两张图供您参考:
这是第一个活动的代码:
public class FirstActivity extends Activity {
@Override
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button1).setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0){
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intent);
}
});
}
}
这是第二个活动的代码: / p>
public class SecondActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
// TODO:hide keyboard here
final ProgressDialog dialog = ProgressDialog.show(this,,Please wait ...,true,false,null) ;
//在实际代码中,这里有一个AsyncTask做的东西...
new Handler()。postDelayed(new Runnable(){
@Override
public void run(){
dialog.dismiss();
}
},5000);
}
}
谢谢
使用由phalt发布的技术的变体解决:
InputMethodManager im =(InputMethodManager)this.getApplicationContext()。getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(getWindow()。getDecorView()。getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
此代码在 onCreate
/ onStart
/ onResume
,因为不依赖于集中的视图来获取窗口标记。
I'm having trouble with the on screen keyboard. I have an activity with an EditText
which shows the keyboard, and a button to go to a second activity. The second activity shows a ProgressDialog
on its onCreate()
, does stuff, and dismisses the ProgressDialog
. The problem is that while the ProgressDialog
is displayed, so is the keyboard.
I would like the keyboard to disappear before creating the ProgressDialog
. I searched thorougly both StackOverflow and other sites, but nothing seems to work with this particular scenario.
I'm attaching two pics for your reference:
http://i45.tinypic.com/2rzq7b6.png http://i45.tinypic.com/34ret1z.png
This is the code of the first activity:
public class FirstActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}
and this is the code of the second activity:
public class SecondActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
// TODO: hide keyboard here
final ProgressDialog dialog = ProgressDialog.show(this, "", "Please wait...", true, false, null);
// in real code, here there is an AsyncTask doing stuff...
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
dialog.dismiss();
}
}, 5000);
}
}
Thanks
Solved using a variation of the technique posted by phalt:
InputMethodManager im = (InputMethodManager) this.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
This code works correctly during onCreate
/onStart
/onResume
, since doesn't rely on a focused view to get the window token from.
这篇关于调用显示ProgressDialog的新Activity后隐藏键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!