Android開發關於AlertDialog兩三事
開發android都知道,要顯示視窗訊息,不是那直覺方便的,需要搭配Handler參與才能走得順。
# 原本以為myAlterDialog.show()後,就可以馬上執行其他程式商業邏輯,完成後停掉myAlterDialog即可
# (例如我們要實現APP「顯示"請稍後",然後背景執行其他程式商業邏輯,執行完取消"請稍後"」)
# 但我沒想到程式商業邏輯需要寫在Handler裡面才不會讓卡住。
public class MainActivity extends AppCompatActivity {
AlertDialog.Builder waitBuilder = null;
AlertDialog waitDialog;
...
protected void onCreate(Bundle savedInstanceState) {
...
waitBuilder = new AlertDialog.Builder(activity);
waitBuilder.setCancelable(false);
waitBuilder.setView(R.layout.loading_dialog);
myAlterDialog = waitBuilder.create();
}
public void onStart() {
adapter.setOnItemLongClickListener(.....){
...
runOnUiThread(new Runnable() {
@Override
public void run() {
myAlterDialog.show();
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
...
Toast.makeText(activity,"success"....
myAlterDialog.dismiss();
}
});
}
}, 300);
}
}
...
});
});
}