我的Android应用程序在TabHost中运行。我有5个“子活动”,所有这些都按预期运行,但有一个例外。最后一项是搜索功能。这是一个简单的界面,只有一个标签的TextView,一个用于搜索字段的EditText和一个Button。
当我第一次启动应用程序并导航到“搜索”选项卡时,无论我点击EditText字段多少次,都不会调用软键盘;但是,如果调用了另一个不属于我的TabHost的活动,然后我返回搜索活动并单击Search EditText,则会按预期调用键盘。
我没有任何动作处理程序,只有按下按钮。我试图强迫EditText专注于onCreate事件,甚至强迫键盘使用StackOverflow上的不同方法显示。请参阅下面的我当前的代码:
public class Search extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
//Give the edit text focus
EditText et1 = (EditText) findViewById(R.id.editText1);
et1.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(et1, InputMethodManager.SHOW_FORCED);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
EditText et = (EditText) findViewById(R.id.editText1);
String term = et.getText().toString();
if (term.length() > 0) {
//do stuff }
else if (term.length() <= 0) {
Toast.makeText(getApplicationContext(), "Please Enter A Search Terml", Toast.LENGTH_SHORT).show();
}
}
});
}
以下是TabHost的样子:
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
intent = new Intent().setClass(this, Tab3.class);
// Initialize a TabSpec for each tab and add it to the TabHost
try {
//home Tab
intent = new Intent().setClass(this, Home.class);
spec = tabHost.newTabSpec("home").setIndicator("Home",
res.getDrawable(R.drawable.grade))
.setContent(intent);
tabHost.addTab(spec);
//tab 2
intent = new Intent().setClass(this, Tab2.class);
spec = tabHost.newTabSpec("tab2").setIndicator("Tab2",
res.getDrawable(R.drawable.chart))
.setContent(intent);
tabHost.addTab(spec);
//tab 3
intent = new Intent().setClass(this, Tab3.class);
spec = tabHost.newTabSpec("tab3").setIndicator("Tab3",
res.getDrawable(R.drawable.tab3))
.setContent(intent);
tabHost.addTab(spec);
//tab 4
intent = new Intent().setClass(this, Tab4.class);
spec = tabHost.newTabSpec("tab4").setIndicator("Tab4",
res.getDrawable(R.drawable.tab4))
.setContent(intent);
tabHost.addTab(spec);
//search tab
intent = new Intent().setClass(this, Search.class);
spec = tabHost.newTabSpec("search").setIndicator("Search",
res.getDrawable(R.drawable.magnify))
.setContent(intent);
tabHost.addTab(spec);
//Set tab To Home
tabHost.setCurrentTab(0);
}
catch (Exception ex) {
Toast t = Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_LONG);
t.show();
}
提前感谢您的帮助。
答案 0 :(得分:0)
你可以为你的editText字段尝试onFocusChage监听器,我调用searchBoxEditText ...调用inputmethodmanager,当焦点变为你的编辑文本时打开键盘,然后当它失去焦点时,关闭键盘
onFocusChange(View v, boolean hasFocus){
if(v==searchBoxEditText){
if(hasFocus==true){
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
.showSoftInput(myEditText, InputMethodManager.SHOW_FORCED);
}else{ //ie searchBoxEditText doesn't have focus
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
}
}
}//end onFocusChange