2016-03-11 35 views
1

當我的應用程序啓動時,軟鍵盤默認加載。我這樣做爲什麼不是我的editText.requestFocus();加工?

final EditText editText = (EditText) findViewById(R.id.editText); 
editText.requestFocus(); 

在我的onCreate方法。在我的XML中,我的editText有android:inputType="phone",所以這就是鍵盤出現的原因。

當用戶滾動列表視圖我,鍵盤消失了,這是我做的,也是在的onCreate:

if (scrollState !=0){ 
    InputMethodManager inputMethodManager = (InputMethodManager) 
      getSystemService(Activity.INPUT_METHOD_SERVICE); 
    inputMethodManager.hideSoftInputFromWindow(listview.getWindowToken(), 0); 
} 

現在我已經把進入的ImageButton我的應用程序。當用戶點擊它時,我想讓鍵盤重新啓動,但它不起作用。 ,爲什麼不知道,因爲它似乎是一個非常基本的指令:

public void softkeyboardButton(View v) 
{ 

    final EditText editText = (EditText) findViewById(R.id.editText); 
    editText.requestFocus(); 
    Log.e("button", "Yep, it worked."); 

} 

這裏是我的全部代碼:

package com.example.chris.sunil_gupta; 

import android.app.ActionBar; 
import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.provider.CallLog; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.util.Log; 
import android.view.View; 
import android.view.WindowManager; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.AbsListView; 
import android.widget.ArrayAdapter; 
import android.widget.EditText; 
import android.widget.FrameLayout; 
import android.widget.LinearLayout; 
import android.widget.ListView; 
import android.widget.NumberPicker; 

public class MainActivity extends Activity { 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

//create a ListView object called listview 
     final ListView listview; 


     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     listview = (ListView) findViewById(R.id.ListView_2); 

     String[] countries = {"Ireland", "France","England","Ireland", "France","England","Ireland", "France","England","Ireland", "France","England"}; 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, countries); 

     listview.setAdapter(adapter); 

     final EditText editText = (EditText) findViewById(R.id.editText); 
     editText.requestFocus(); 


listview.setOnScrollListener(new AbsListView.OnScrollListener() { 
    @Override 
    public void onScrollStateChanged(AbsListView view, int scrollState) { 



     Log.e("scrollState", "Yep, it worked."); 


     if (scrollState !=0){ 
      InputMethodManager inputMethodManager = (InputMethodManager) 
        getSystemService(Activity.INPUT_METHOD_SERVICE); 
      inputMethodManager.hideSoftInputFromWindow(listview.getWindowToken(), 0); 
     } 


    } 
// this method looks for changes in the edittext box. 
    @Override 
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 

    } 
}); 



     editText.addTextChangedListener(new TextWatcher() { 


      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 

      } 

      @Override 
//   If there are changes typed in the edittext, then change the height of the edittext from 0, 
//   as described in the activity_main xml file 
      public void afterTextChanged(Editable s) { 

       float density=getResources().getDisplayMetrics().density; 

       if (editText.length() > 0) { 


//     not sure what density and layoutparams do. I think we're 
//     converting pixels to dp 
        editText.getLayoutParams().height =(int)(50*density); 
//     editText.requestLayout(); 
        Log.e("scrollState", "Yep, it worked."); 

       } 
//    if there is nothing in edittext, make it invisible 
       else if (editText.length() == 0){ 

        editText.getLayoutParams().height =(int)(0*density); 

       } 
       editText.requestLayout(); 

      } 
     }); 





    } 

    public void softkeyboardButton(View v) 
    { 

     final EditText editText = (EditText) findViewById(R.id.editText); 
     editText.requestFocus(); 
     Log.e("button", "Yep, it worked."); 

    } 

    //This clears the edittext next time user starts the application, rather than 
// having the same numbers there, which the user probably doesn't want anymore 
    protected void onResume() { 
     final EditText editText = (EditText) findViewById(R.id.editText); 
     super.onResume(); 
     editText.setText(""); 


    } 

    } 
+1

嘗試使用inputmethodmanager的showSoftInput函數,而不是按鈕單擊時的edittext上的requestFocus。 –

+1

InputMethodManager imm =(InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE); imm.showSoftInput(ed,0); ed.requestFocus(); – saeed

+0

是的,用過。但它使鍵盤彈出兩次 - 當我關閉我的應用程序並顯示QWERTY鍵盤時,它仍然存在。我認爲我想這樣做的方式會很簡單。 – CHarris

回答

1

試試這個

作秀鍵盤

InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE); 
imm.showSoftInput(ed, 0); 
ed.requestFocus(); 

希望這可以幫到你