2012-10-29 58 views
16

我有一個edittext和一個微調。當我觸摸編輯文本時,出現鍵盤,完成文本編輯後,我觸摸微調器的下拉箭頭,但鍵盤不會自動消失。請給我一些解決方案。 我想這個代碼如何隱藏虛擬鍵盤觸摸旋鈕

InputMethodManager imm=(InputMethodManager)getApplicationContext().getSystemService(SetUpProfileActivity.this.INPUT_METHOD_SERVICE); 
     imm.hideSoftInputFromWindow(mDateOfBirth.getWindowToken(), 0); 

這是XML

<LinearLayout 
      android:id="@+id/outerlayout" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:padding="10dp" 

      > 

      <TextView 
       android:id="@+id/name_view" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/profile_name" 
       android:textColor="#ffffff" /> 

      <EditText 
       android:id="@+id/profile_name" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="5dp" 
       android:background="@drawable/txtbox" 
       android:singleLine="true" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="5dp" 
       android:text="@string/dateofbirth" 
       android:textColor="#ffffff" /> 

      <Spinner 
       android:id="@+id/dob" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="5dp" 
       android:background="@drawable/dropdown" /> 
+0

Softkeyboard自動消失的是當你的EditText失去焦點..所以可能是你使用任何其他財產的EditText,顯示你的XML代碼: –

+0

@CapDroid我已經發布我的XML,請讓我知道我我錯了。 – swati

回答

31

試試這個代碼,我希望它會爲你工作。

mSpinner.setOnTouchListener(new OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       InputMethodManager imm=(InputMethodManager)getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
       imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0); 
       return false; 
      } 
     }) ; 
+0

@swati好:)親愛的親愛的 –

0

試試這個:

// hide the keyboard if open 
      inputManager.hideSoftInputFromWindow(getParent().getCurrentFocus() 
        .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
+0

這是我用來設置spinner.Can代碼你建議我在哪裏,我應該用你的代碼ArrayAdapter dataAdapterage =新ArrayAdapter (這一點, \t \t R.layout.spinner_adapter,ageValue); dataAdapterheight.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mDateOfBirth.setAdapter(dataAdapterage); – swati

+0

你應該在spinner的點擊監聽器中寫下這個 –

0

如果你在一個活動有微調和EditText上那麼這樣的問題,你是會覺得在您的微調和內部的肯定,

通話onTouchListener把你的EditText的參考和隱藏softkeyboard 。

mySpinner.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      InputMethodManager inputMethodManager=(InputMethodManager)getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
      inputMethodManager.hideSoftInputFromWindow(mReuestBloodActNotes.getWindowToken(), 0); 
      return false; 
     } 
    }) ;