我有一個AlertDialog
,其中包含幾個EditText
意見。當用戶點擊清除按鈕時,應從文本中清除當前具有焦點的視圖。如何獲得焦點在AlertDialog中的當前視圖?
我使用的方法getCurrentFocus()
它返回一個視圖並將其轉換爲EditText
。但是,該方法始終返回null。
下面是我使用的代碼:
DialogBuilder.setNeutralButton("Clear", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
((EditText) this.getCurrentFocus()).setText("");
}
})
您能得到什麼,如果你把它稱爲對話框呢? '((EditText)((Dialog)dialogInterface).getCurrentFocus())。setText(「」);' –
@DanielZolnai按照你的建議,將'dialogInterface'作爲'Dialog'投射出來。我只需要修復paranthesis。謝謝。 – yogur
添加了註釋作爲答案 –