android中自訂的dialog中的EditText無法彈出IME解決方案

來源:互聯網
上載者:User

標籤:

1.解決無法彈出IME:

在show()方法調用之前,用dialog.setView(new EditText(context))添加一個空的EditText,由於是自訂的AlertDialog,有我們指定的布局,所以設定這個不會影響我們的功能,這樣就可以彈出IME了……

2.可以彈出IME了,但了為了增強使用者體驗性,當dialog中含有editText時應該,在顯示dialog的同時自動彈出鍵盤:
(1) 可以在自訂的dialog中增加如下方法: [java] view plain copy  
  1. public void showKeyboard() {  
  2.         if(editText!=null){  
  3.             //設定可獲得焦點  
  4.             editText.setFocusable(true);  
  5.             editText.setFocusableInTouchMode(true);  
  6.             //請求獲得焦點  
  7.             editText.requestFocus();  
  8.             //調用系統IME  
  9.             InputMethodManager inputManager = (InputMethodManager) editText  
  10.                     .getContext().getSystemService(Context.INPUT_METHOD_SERVICE);  
  11.             inputManager.showSoftInput(editText, 0);  
  12.         }  
  13.     }  
其中editText為自訂dialog中的輸入框的view
(2) 在dialog.show()後,調用這個方法顯示IME,由於在調用時可能dialog介面還未載入完成,editText 可能還為空白,所以需要加上一個延時任務,延遲顯示: [java] view plain copy  
  1. dialog.show();  
  2. Timer timer = new Timer();  
  3. timer.schedule(new TimerTask() {  
  4.   
  5.     @Override  
  6.     public void run() {  
  7.         dialog.showKeyboard();  
  8.     }  
  9. }, 200);  


android中自訂的dialog中的EditText無法彈出IME解決方案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.