我有自定義listview
與editText
和編輯edittext
數據點擊edittext
幫助顯示鍵盤它工作正常。如何在editText外部點擊時隱藏鍵盤?
我的問題是當我點擊edittext
之外的鍵盤時必須隱藏。
感謝...
我有自定義listview
與editText
和編輯edittext
數據點擊edittext
幫助顯示鍵盤它工作正常。如何在editText外部點擊時隱藏鍵盤?
我的問題是當我點擊edittext
之外的鍵盤時必須隱藏。
感謝...
爲此,你必須採取onTouchListener上佈局文件的父佈局。在TouchListener上,當在EditText外單擊時,必須編碼以隱藏鍵盤。請遵循XML佈局和Java類來解決這個問題,請遵循以下網址。
http://amitthaperandroidquery.blogspot.com/2011/10/remove-keyboard-after-click-outside.html
的一種方法是,你可以設置一個焦點變化監聽到的EditText。 當控件失去焦點,您可以通過隱藏Keyborad: -
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
et.setOnFocusChangeListener(新OnFocusChangeListener(){ \t \t \t \t @覆蓋 \t \t \t \t公共無效onFocusChange (視圖v,布爾hasFocus){ \t \t \t \t \t \t \t \t \t \t Log.d(「onChange」,「focus ==」+ hasFocus); }}); – Archana
當我點擊像列表視圖之外的edittext然後它不響應 – Archana
edittext不會改變焦點,當用setOnFocusChangeListener點擊它外面。它只會在將焦點轉換爲其他可聚焦的其他內容時才起作用,如另一個編輯文本。 – Karl
您可以通過執行以下步驟實現:
讓(你的活動內容視圖)的父視圖點擊和可聚焦通過添加以下屬性
android:clickable="true"
android:focusableInTouchMode="true"
執行hideKeyboard()方法
public void hideKeyboard(View view) {
InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
最後,設置edittext的onFocusChangeListener。
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
另一種方式類似於接受的答案是繼承佈局的根視圖,並覆蓋OnInterceptTouchEvent,隱藏鍵盤並返回false允許觸摸傳播如常。
下面的例子是Xamarin但很容易移植到Java:
public class KeyboardHidingScrollView : ScrollView
{
public KeyboardHidingScrollView (Context context) : base (context)
{
}
public KeyboardHidingScrollView (Context context, IAttributeSet attrs) : base (context, attrs)
{
}
public override bool OnInterceptTouchEvent (Android.Views.MotionEvent ev)
{
var methodManager = (InputMethodManager)Context.GetSystemService (Context.InputMethodService);
methodManager.HideSoftInputFromWindow (WindowToken, HideSoftInputFlags.None);
return false;
}
}
如果您需要更多幫助,請查詢我的博客網址或發表評論。 –
謝謝你,它很好地工作.. – Archana