开发者

EditText box will not invoke the soft keyboard in many circumstances

开发者 https://www.devze.com 2023-04-09 22:14 出处:网络
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

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 nav开发者_StackOverflow社区igate 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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号