我在Android的非常新,我有一個問題,請大家看看下面的代碼:爲什麼下一條指令立即執行?
new AlertDialog.Builder(this)
.setTitle("Save File")
.setMessage("Do you want to save to a file ?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Ask user for a file name to save the text in the text editor.
PreSaveFile();
// Clear text editor.
// This is executed immediately and it does not wait until PreSaveFile() ends.
CleanEditor(et);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
CleanEditor(et);
}
})
的問題是,第二個電話,CleanEditor(et)
,不等到PreSaveFile()
結束,所以當我選擇一個文件名來保存我在文本編輯器中的內容,CleanEditor(et)
刪除的文本編輯器,所以什麼都不能保存。
我希望我已經夠清楚了。
PreSaveFile()是做什麼的?你可以發佈代碼,以及你如何知道它沒有等待函數完成? – ChrisHarris2012 2012-08-13 18:48:24
Private void PreSaveFile(){ \t EditText et =(EditText)findViewById(R.id.editText1);如果(et.length()== 0){ \t \t Toast.makeText(getBaseContext(),「Editor is empty!」,Toast.LENGTH_SHORT).show(); \t} \t \t如果(textFile.length()== 0){ \t \t意圖I =新意圖(MainActivity.this,FileChooser.class); \t \t \t \t Bundle myData = new Bundle(); \t \t \t myData.putString(「textFile」,「」); \t \t \t i.putExtras(myData); \t \t \t startActivityForResult(i,1); \t} else {SaveFile(); } \t} 顯示文件瀏覽器,以便您可以選擇一個文件來編寫文本編輯器的內容 – 2012-08-13 21:25:05