2012-06-23 30 views
1

我想在alertbox中設置textview文本。我想製作倒計時框。我的代碼如下: 我的自定義警報框。alertview box沒有顯示在listviewitem上點擊android

public abstract class ChallengeDialog extends AlertDialog.Builder implements OnClickListener { 

    TextView msg; 

/** 
    * @param context 
    * @param title resource id 
    * @param message resource id 
    */ 
public ChallengeDialog(Context context, String title, String message) { 
    super(context); 
    setTitle(title); 
// setMessage(message); 
    msg = new TextView(context); 
    msg.setText(message); 
    setView(msg); 

    setPositiveButton("accept", this); //In android this is OK button 
    setNegativeButton("reject", this);//In android this is cancel button 
} 

public void setDialogMsg(String m){ 
    msg.setText(m); 
    setView(msg); 
} 

/** 
    * will be called when "cancel" pressed. 
    * closes the dialog. 
    * can be overridden. 
    * @param dialog 
    */ 
//This is cancel button 
public void onOkClicked(DialogInterface dialog) { 
    dialog.dismiss(); 
} 

@Override 
public void onClick(DialogInterface dialog, int which) { 

    if (which == DialogInterface.BUTTON_POSITIVE) { 

    dialog.dismiss(); 

    } else { 

     onOkClicked(dialog); 

    } 

} 

/** 
    * called when "ok" pressed. 
    * @param input 
    * @return true, if the dialog should be closed. false, if not. 
    */ 
//This is challenge button 
abstract public boolean onCancelClicked(String input); 

} 

代碼使用

ChallengeDialog chlngDialog = new ChallengeDialog(MyActivity.this,"title","count") { 

          @Override 
          public boolean onCancelClicked(String input) { 
           // TODO Auto-generated method stub 
           return false; 
          } 

         }; 

         AlertDialog a = chlngDialog.show(); 


         for(int i = 15 ; i>0 ;i--){ 

          try { 
           chlngDialog.setDialogMsg("count "+i); 
           Thread.sleep(1000); 

          } catch (InterruptedException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 
         } 
//a.dismiss(); 

我在列表視圖中點擊執行此代碼自定義警告框。但是警報框在15秒後顯示,我點擊了listview項目。我不知道爲什麼是這樣?

回答

0

你把for循環放在onItemClick的監聽器中嗎?

如果是,則在onItemClick偵聽器中創建一個異步任務並執行該任務。將for循環放入doInBackground

1

我已經結束了與Android Coader建議的異步任務。以下代碼如下:

public abstract class MyDialog extends AlertDialog.Builder implements OnClickListener { 

    TextView msgTxt; 
    private static int count= 15 ; 
    private String user; 
    private int c; 

/** 
    * @param context 
    * @param title resource id 
    * @param message resource id 
    */ 
public ChallengeDialog(Context context, String title, String message) { 
    super(context); 
    setTitle(title); 
    setCancelable(false); 
// setMessage(message); 
    msgTxt = new TextView(context); 
    msgTxt.setText(message); 
    setView(msgTxt); 

    setPositiveButton("accept", this); //In android this is OK button 
    setNegativeButton("reject", this);//In android this is cancel button 

    MyTimerTask myTask = new MyTimerTask(); 
    myTask.execute(new String[] { "abc" }); 

} 

public void setUserAndBet(String u,int b){ 
     c= b; 
     user = u ; 
} 

/** 
    * will be called when "cancel" pressed. 
    * closes the dialog. 
    * can be overridden. 
    * @param dialog 
    */ 

@Override 
public void onClick(DialogInterface dialog, int which) { 

    if (which == DialogInterface.BUTTON_POSITIVE) { 

     onAcceptClicked(dialog); 
// dialog.dismiss(); 

    } else { 

     onRejectClicked(dialog); 

    } 

} 

/** 
    * called when "ok" pressed. 
    * @param input 
    * @return true, if the dialog should be closed. false, if not. 
    */ 
//This is accept button 
abstract public boolean onAcceptClicked(DialogInterface d); 
abstract public boolean onRejectClicked(DialogInterface d); 

    Handler mHandler = new Handler() { 
    @Override 
    public void handleMessage(Message msg) { 

     int countDownValue = (Integer)msg.obj; 

     msgTxt.setText(countDownValue +" seconds"); 

     //call setText here 
    } 
    }; 


class MyTimerTask extends AsyncTask<String, Void, String>{ 

     @Override 
     protected String doInBackground(String... urls) { 
      //mHandler.sendEmptyMessage(0); 

      while(count > 0){ 

       Message msg = new Message(); 
       int countDownValue = count--; 
       msg.obj = countDownValue; 
       mHandler.sendMessage(msg); 
       try { 
       Thread.sleep(1000); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      } 

      return " "; 


     } 
} 
} 

只需創建此調用的對象並調用show()方法即可。謝謝Adndroid Coader