对话框按钮,具有倒计时默认按钮

时间:2013-08-26 14:59:06

标签: android jquery dialog alertdialog countdowntimer

如何为用户创建对话框,而不是一般对话框, 有按钮\按钮,其中一个将是默认答案, 这个按钮将按时倒计时。

  

例如:

对话框:“嗨,你可能..............?” (是/否)

否则会在秒数后自动接受。

  

这是另一个示例

像这样:     like this

1 个答案:

答案 0 :(得分: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>

现在开始构建一个包含对话框数据的抽象类,例如标题,消息和按钮文本......等等

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;
}

}

创建类视图持有者,负责更新对话框客户视图组件

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活动中的

创建你的客户对话视图,并且不要设置对话框监听器来处理对话框点击。

   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