2013-08-26 148 views
0

如何爲用戶創建對話框,但不是一般對話框, 有按鈕\按鈕,其中一個將默認答案, ,此按鈕將打開倒計時的時間。帶有倒計時的默認按鈕對話框按鈕

例如:

對話框: 「嗨,你可以..............?」 (是/否)

在秒數之後否將自動接受。

這是一個更例如

這樣的: like this

回答

2

這可能是完美的答案,但我認爲它會爲你工作,你可以定製你的代碼如所須。

創建具有dailog自定義圖的佈局,將其命名(my_dialog_layout.xml):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/my_dialog_layout_message_text_view" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:layout_margin="10dp" 
     android:text="My message" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="1.0" > 

     <Button 
      android:id="@+id/my_dialog_layout_positive_bttn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" 
      android:text="YES" /> 

     <Button 
      android:id="@+id/my_dialog_layout_negative_bttn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" 
      android:text="NO" /> 

    </LinearLayout> 

</LinearLayout> 

現在開始builing持有你的對話框的數據,如標題,消息的抽象類和按鈕文本...等

package com.example.timerdialog; 

public abstract class MyDialog 
{ 
    private String title; 
    private String message; 
    private String positiveBttnText; 
    private String negativeBttnText; 
    private int time;//time in seconds 

public String getTitle() 
{ 
    return title; 
} 
public void setTitle(String title) 
{ 
    this.title = title; 
} 
public String getMessage() 
{ 
    return message; 
} 
public void setMessage(String message) 
{ 
    this.message = message; 
} 
public String getPositiveBttnText() 
{ 
    return positiveBttnText; 
} 
public void setPositiveBttnText(String positiveBttnText) 
{ 
    this.positiveBttnText = positiveBttnText; 
} 
public String getNegativeBttnText() 
{ 
    return negativeBttnText + "(" + time + ") "; 
} 
public void setNegativeBttnText(String negativeBttnText) 
{ 
    this.negativeBttnText = negativeBttnText; 
} 
public int getTime() 
{ 
    return time; 
} 
public void setTime(int time) 
{ 
    this.time = time; 
} 

}

創建類的觀點持有者,將採取更新的對話框custome視圖組件

package com.example.timerdialog; 

import android.app.Dialog; 
import android.content.Context; 
import android.os.Handler; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class MyDialogView extends MyDialog 

{ 
private Context    context; 
private View    rootView; 
private DialogListener  dialogListener;// listener to simulate click events 
private Dialog    dialog; 

private TextView   messageTextView; 
private Button    positiveBttn; 
private Button    negativeBttn; 

private Handler    handler;// handler will be use as timer 
private Runnable   runnable; 

private int defaultTime; 

/** 
* 
* @param context 
* @param title 
* @param message 
* @param positiveBttnText 
* @param negativeBttnText 
* @param defaultTime 
*/ 
public MyDialogView(Context context,String title,String message,String positiveBttnText,String negativeBttnText,int defaultTime) 
{ 
    this.context = context; 
    this.defaultTime = defaultTime; 
    setTitle(title); 
    setMessage(message); 
    setPositiveBttnText(positiveBttnText); 
    setNegativeBttnText(negativeBttnText); 
    setTime(defaultTime); 
} 

private void buildUi() 
{ 
    if(rootView!=null) 
    { 
     messageTextView = (TextView) rootView.findViewById(R.id.my_dialog_layout_message_text_view); 
     positiveBttn = (Button) rootView.findViewById(R.id.my_dialog_layout_positive_bttn); 
     negativeBttn = (Button) rootView.findViewById(R.id.my_dialog_layout_negative_bttn); 

     messageTextView.setText(getMessage()); 
     positiveBttn.setText(getPositiveBttnText()); 
     negativeBttn.setText(getNegativeBttnText()); 

     positiveBttn.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       if(dialogListener!=null) 
       { 
        if(handler!=null) handler.removeCallbacks(runnable);//remove any queued post 
        setTime(defaultTime);//reset the default time 
        dialogListener.onPositiveClick(); 
       } 
      } 
     }); 

     negativeBttn.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       if(dialogListener!=null) 
       { 
        if(handler!=null) handler.removeCallbacks(runnable);//remove the previous post 
        setTime(defaultTime);//reset the default time 
        dialogListener.onNegativeClick(); 
       } 
      } 
     }); 

     startHandler(); 
    } 
} 

public void setOnDialogListener(DialogListener listener) 
{ 
    dialogListener = listener; 
} 
public interface DialogListener 
{ 
    public abstract void onPositiveClick(); 
    public abstract void onNegativeClick(); 
} 
/** 
* build and show dialog 
*/ 
public void show() 
{ 
    rootView = View.inflate(context, R.layout.my_dialog_layout, null); 
    dialog = new Dialog(context); 
    handler = new Handler(); 
    buildUi(); 
    dialog.setTitle(getTitle());// set title for dialog 
    dialog.setContentView(rootView);// set the content view of the dialog 
    dialog.setCancelable(false);// set the dialog to be non-cancelable 
    dialog.show(); 
} 

public void dismissDialog() 
{ 
    if(dialog!=null) dialog.dismiss(); 
} 

private void startHandler() 
{ 
    runnable = new Runnable() 
    { 

     @Override 
     public void run() 
     { 
      int time = getTime(); 
      setTime(time - 1); 
      if (time <= 0)// if time is less than or equal to zero then stop the handler 
      { 
       handler.removeCallbacks(this);//remove any queued post 
       setTime(defaultTime);// reset the default time 
       if(dialogListener != null) dialogListener.onNegativeClick();// simulate negative button click 
      } 
      else 
      { 
       buildUi();// rebuild the ui of the dialog 
      } 

     } 
    }; 
    if (handler != null) 
    { 
     handler.postDelayed(runnable, 1000);// send post after 1 second = 1000 ms 
    } 
} 

}

在你的Android活動創建您的custome對話視圖的照顧,不foget設置對話框偵聽器來處理對話框點擊。

final MyDialogView myDialogView = new MyDialogView(MainActivity.this, "My Dialog", "This is test message", "YES", "NO",10); 
    findViewById(R.id.button1).setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      myDialogView.show(); 
     } 
    }); 


myDialogView.setOnDialogListener(new DialogListener() 
{ 

    @Override 
    public void onPositiveClick() 
    { 
     myDialogView.dismissDialog(); 
    } 

    @Override 
    public void onNegativeClick() 
    { 
     myDialogView.dismissDialog(); 
    } 
}); 

- 結果

enter image description here

+0

感謝了很多!你是最好的,我現在就試試吧! :) –

+0

非常感謝你。效果最好! –