0

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

例如:

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

秒数后将自动接受否。

这是另一个例子

像这样: 像这样

4

1 回答 1

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

- 结果

在此处输入图像描述

于 2013-08-26T16:28:15.347 回答