在许多情况下,EditText 框不会调用软键盘

发布于 2024-12-07 04:14:43 字数 3507 浏览 1 评论 0原文

我的 Android 应用程序在 TabHost 中运行。我有 5 个“子活动”,除了一个例外之外,所有这些都按预期运行。最后一项是搜索功能。这是一个非常简单的界面,只有一个用于标签的 TextView、一个用于搜索字段的 EditText 和一个按钮。

当我第一次启动应用程序并导航到“搜索”选项卡时,无论我单击 EditText 字段多少次,都不会调用软键盘;但是,如果调用不属于 TabHost 一部分的另一个活动,然后我返回到“搜索”活动并单击“搜索 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();
    }

提前感谢您的帮助。

My Android application runs within a TabHost. I have 5 "sub-activities" all of which run as expected with one exception. The last item is a search feature. It is such a simple interface with just a TextView for a label, an EditText for the search field and a Button.

When I launch the app for the first time and navigate to the Search tab, no matter how many times I click into the EditText field, the soft keyboard is not invoked; however, if another activity that is not a part of my TabHost is called and then I return to the Search activity and click on the Search EditText, the keyboard is invoked as expected.

I don't have any action handlers for the field, only for the button press. I have tried forcing the EditText to get focus on the onCreate event and even forcing the keyboard to show using different methods found here on StackOverflow. See my current code below:

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();
            }

        }

    });

    }

Here's what the TabHost looks like:

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();
    }

Thanks in advance for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲影 2024-12-14 04:14:43

您可以尝试为您的 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

you might try a onFocusChage listener for your editText field which i called searchBoxEditText... call inputmethodmanager to open the keyboard when focus is changed to your edit text, an then when it looses focus, close the keyboard

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文