2015-07-10 85 views
6

問題:無法隱藏Android的軟鍵盤,即使輸入管理

我想按下按鈕「添加」時,隱藏鍵盤。屏幕上有兩個EditText。鍵盤不會在開始活動時出現,這很好,但它不會在點擊按鈕時消失。

enter image description here

下面是堆棧溢出所有可能的問題,我已經看到其答案不幫我:

Close/hide the Android Soft Keyboard

Programmatically Hide/Show Android Soft Keyboard

How to hide Soft Keyboard when activity starts

How to hide soft keyboard on android after clicking outside EditText?

等等。

這裏是我的代碼:

AddActivity

public class AddActivity extends ActionBarActivity { 
EditText text1,text2; 
DbHelper db; 
ListView l; 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_add); 
    db = new DbHelper(this); 
    l = (ListView) findViewById(R.id.listInAddActivity); 
    text1 = (EditText) findViewById(R.id.i1); 
    text2 = (EditText) findViewById(R.id.i2); 
//  text1.setInputType(InputType.TYPE_NULL); 
    //  text2.setInputType(InputType.TYPE_NULL); 
    hideKeyboard(); 

    loadDataInAdd(); 

} 
public void addNewTask(View view) { 
    String s1 = text1.getText().toString(); 
    String s2 = text2.getText().toString(); 
    db.addData(s1,s2); 
    loadDataInAdd(); 
    hideKeyboard(); 
} 
public void loadDataInAdd() 
{ 
    try { 
     Cursor cursor = db.fetchData(); 

     ListAdapter myAdapter = new SimpleCursorAdapter(this, R.layout.tasks, 
       cursor, 
       new String[]{db._ID, db.COLUMN_1, db.COLUMN_2}, 
       new int[]{R.id.idnum, R.id.c1, R.id.c2}, 0); 
     l.setAdapter(myAdapter); 
    } 
    catch(NullPointerException e) 
    { 
     e.printStackTrace(); 
    } 
    // MainActivity.loadData(); 
} 
private void hideKeyboard() { 
    // Check if no view has focus: 
    View view = this.getCurrentFocus(); 
    if (view != null) { 
     InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE); 
     inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
    } 
} 

} 

大多數的答案是關於InputManager方法,但在點擊時,它不適合我的工作,即不隱藏鍵盤按鈕。下面是我用的InputManager另一個變化:

View view = this.getCurrentFocus(); 
    if (view != null) { 
     InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE); 
     inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0); 

我也試過這樣:

getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN 
); 

,這不工作,要麼(鍵盤是不是隱藏在點擊按鈕)。

我也曾嘗試:

 <activity android:name=".AddActivity" 
       android:label="@string/app_name" 
       android:parentActivityName=".MainActivity" 
     android:windowSoftInputMode="stateAlwaysHidden"> 

    </activity> 

<activity android:name=".AddActivity" 
       android:label="@string/app_name" 
       android:parentActivityName=".MainActivity" 
     android:windowSoftInputMode="stateAlwaysHidden"> 

    </activity> 

有了這一個,我的應用程序停止工作:

InputMethodManager inputManager = (InputMethodManager) 
      getSystemService(Context.INPUT_METHOD_SERVICE); 

    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 
      InputMethodManager.HIDE_NOT_ALWAYS); 

這一個完全隱藏鍵盤(鍵盤做即使點擊了editText也不會出現):

text1.setInputType(InputType.TYPE_NULL); 
text2.setInputType(InputType.TYPE_NULL); 

回答

6

我決定使用onclicklistener我的按鈕,而不是使用其他功能,並通過onClickAddActivity.xml調用它。

當我決定嘗試再次嘗試使用OnCliCkListener時,我的一半是通過我的問題。幾個隨機嘗試後,下面是最後的代碼,爲我的作品:

btn.setOnClickListener(new View.OnClickListener() { 


     public void onClick(View v) { 
      // TODO Auto-generated method stub 

       InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); 
       inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); 

     } 
    }); 

另一種方式是通過以下方式使用OnClickListener

private View.OnClickListener mListener = new View.OnClickListener() { 
    public void onClick(View v) { 
     // do something when the button is clicked 

     //public void onClick(View v) { 

      try { 
       InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); 
       inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); 
      } 
catch (Exception e) { 
     e.printStackTrace(); 
      } 

    } 
}; 

要點初學者和我一樣:

  • 上面的代碼應該放在之前protected void onCreate(Bundle savedInstanceState)
  • 要調用上述代碼,請將此代碼放入protected void onCreate(Bundle savedInstanceState)

    btn.setOnClickListener(mListener);

  • 使用try-catch在這兩種方法的異常處理(onClickListenersetOnClickListener

在尋找爲什麼onClickXML屬性不爲我工作,而OnClickListener呢,我發現以下有用的鏈接:

setOnclickListener vs OnClickListener vs View.OnClickListener

Android onClick in XML vs. OnClickListener

How exactly does the android:onClick XML attribute differ from setOnClickListener?

Difference between OnClick() event and OnClickListener?

現在,中途我的回答,我意識到自己的錯誤,在使用onClick從XML。這裏是我做了什麼,擺脫OnClickListener S的:

  • 首先,我提出我的代碼掩藏鍵盤進入方法onClick的一部分。在我的情況下,我將hideKeyboard()中的整個代碼移動到addNewTask

使用onClick

  • 方法,對於一些重要的點應該是publicvoid

  • 它應該採取View參數,如View V

最後,該代碼的作品:

public void addNewTask(View view) { 
    String s1 = text1.getText().toString(); 
    String s2 = text2.getText().toString(); 
    db.addData(s1, s2); 
    loadDataInAdd(); 
    // hideKeyboard(); below is the code to hide keyboard 
    if (view != null) { 
     InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE); 
     inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0); 
    } 
} 

摘要

我發現3種方式來隱藏軟鍵盤對我的情況下,使用:

  • OnClickListener:

    private View.OnClickListener mListener = new View.OnClickListener() { 
    public void onClick(View v) { 
        // do something when the button is clicked 
    
        //public void onClick(View v) { 
    
         try { 
          InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); 
          inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); 
         } 
    
        catch (Exception e) { 
        e.printStackTrace(); 
         } 
    
    } 
    }; 
    
  • setOnClickListener:

    btn.setOnClickListener(new View.OnClickListener() { 
    
    
        public void onClick(View v) { 
         // TODO Auto-generated method stub 
    
          InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); 
          inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); 
    
        } 
    }); 
    
  • 的onClick中,XML屬性:

    public void addNewTask(View view) { 
    String s1 = text1.getText().toString(); 
    String s2 = text2.getText().toString(); 
    db.addData(s1, s2); 
    loadDataInAdd(); 
    // hideKeyboard(); 
    if (view != null) { 
        InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE); 
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0); 
    } 
    } 
    
+1

我感謝您的回答。但所有的答案已經可用。如果您在_Google_上進行了更好的搜索。但沒關係,一切都在一起。 –

+1

@MD:你可能是對的,因爲我是Android的初學者,所有的部分答案讓我困惑不已。在過去的幾天裏,我經歷了數百個鏈接(大部分來自SO),並試圖實現所有的答案。但他們只是不適合我的情況。例如,'InputMethodManager'是要走的路,幾乎所有的答案都表明,但是你可以看到我的問題是如何失敗的。我必須從這個答案拼湊出一部分,從這個答案的一部分,我自己邏輯的一部分,以及代碼應該到哪裏去,等等。 –

+1

雅這就是爲什麼我寫'讚賞你的答案'。 +1 up –

1

檢查了這一點

View view = this.getCurrentFocus(); 
if (view != null) { 
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 
} 

Close/hide the Android Soft Keyboard