2015-06-29 26 views
0

我知道如果隱藏鍵盤如果類是擴展活動,但是當它擴展DialogFragment我的代碼隱藏鍵盤從活動是行不通的。從對話框中的editText隱藏鍵盤

這是我到目前爲止的代碼:

public class PersonalData extends DialogFragment 
    LinearLayout activity_personaldata; 

//Oncreate: 
activity_personaldata = (LinearLayout) view.findViewById(R.id._activity_personaldata_); 
    activity_personaldata.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); 
      imm.hideSoftInputFromWindow(getActivity().getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY); 
     } 
    }); 

和我的XML是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/_activity_personaldata_" 
android:focusable="true" 
android:focusableInTouchMode="true" 
android:orientation="vertical"> 

<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="8dp"> 
</ScrollView> 
</LinearLayout> 

我必須做出ID在我的XML中的LinearLayout,並給予可獲得焦點。但它仍然沒有工作。 請幫助我,在此先感謝:)

回答

1

試試這個代碼在onCreateView()getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

如果要隱藏鍵盤,當用戶點擊您的EditText之外,可以實現爲EditText一個focusListner

youreditText.setOnFocusChangeListener(new OnFocusChangeListener() {   

     public void onFocusChange(View v, boolean hasFocus) { 
      if (!hasFocus) { 
       InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
       imm.hideSoftInputFromWindow(v.getWindowToken(), 0); 
      } 
     } 
    }); 
+0

對不起,我在這篇文章中的標題是錯誤的。當用戶點擊edittext外部時,我想隱藏鍵盤。並在用戶點擊edittext時再次顯示。我使用linearlayout來隱藏鍵盤。但它不工作.. –

+0

感謝隊友,現在的工作.. –