喜欢Modal Dialog android

时间:2013-11-16 17:51:18

标签: android user-input wait

我希望在继续使用该程序之前获得用户的性别,因此,我使用了Asyc类

 class conectNet extends AsyncTask<Void,Void,Void>

我首先避免在主要布局中使用网络。 然后我显示一个对话框,要求用户识别他的性别:

protected void onPostExecute(Void result){ // this method can use UI as mentioned in its docs
...

...

我的问题的要点

我进行对话并希望停止该线程,直到我收到用户响应:

 ...
    ...
     AlertDialog.Builder builder = new AlertDialog.Builder(SendRandom.this);
    builder.setTitle("gender");
     builder.setMessage(R.string.Gender_dialogQ); //Gender_dialogQ=select your gender!
    // Set up the buttons
     builder.setPositiveButton("Male", new DialogInterface.OnClickListener() { 
       @Override
       public void onClick(DialogInterface dialog, int which) {
         userGender=Gender.MALE;//put value on mainClass proprty (userGender)
    synchronized (Thread.currentThread()){
        Thread.currentThread().notify(); // here is the POINT : resume the thread when selection done
        } } });
        builder.setNegativeButton("Female", new DialogInterface.OnClickListener() {
         @Override
        public void onClick(DialogInterface dialog, int which) {
         userGender=Gender.FEMALE;
         dialog.cancel();
         synchronized (Thread.currentThread()){
        Thread.currentThread().notify();}}});
        builder.show();// show dialogue and then pause the thread by next line
         try {
        synchronized( Thread.currentThread()){
        Thread.currentThread().wait();}
                    } catch......
    ....

这使得android程序可以暂停并关闭

任何建议??

2 个答案:

答案 0 :(得分:1)

您不需要Threadssynchronized等所有内容......只需在AlertDialog.Builder上使用setCancelable即可。这会强制用户在继续或关闭Dialog之前选择并选择。

 AlertDialog.Builder builder = new AlertDialog.Builder(SendRandom.this);
 builder.setTitle("gender");
 builder.setCancelable(false);  // here
 builder.setMessage(R.string.Gender_dialogQ); //Gender_dialogQ=select your gender!
 // Set up the buttons
 ...

答案 1 :(得分:0)

对话框将在UI线程上运行,因为对话框是UI元素。所以,我不确定你在使用AsyncTask和线程的东西。摆脱这一切。