android - dialog buttons with count-down for default button -
how can make dialog user, not general dialog, there button\buttons 1 of them default answer, , button on time count-down.
for example:
dialog: "hi may you..............?" (yes/no)
the no accepted automatically after number of seconds.
this 1 more example:
like this:
this might perfect answer, think work , can customize code needed.
create layout has dailog custom view, name (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>
now start builing abstract class hold dialog data, such title, message, , button texts ... etc
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; }
}
create class views holder take care of updating dialog custome view components
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 simulate click events private dialog dialog; private textview messagetextview; private button positivebttn; private button negativebttn; private handler handler;// handler use 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 queued post settime(defaulttime);//reset default time dialoglistener.onpositiveclick(); } } }); negativebttn.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if(dialoglistener!=null) { if(handler!=null) handler.removecallbacks(runnable);//remove previous post settime(defaulttime);//reset default time dialoglistener.onnegativeclick(); } } }); starthandler(); } } public void setondialoglistener(dialoglistener listener) { dialoglistener = listener; } public interface dialoglistener { public abstract void onpositiveclick(); public abstract void onnegativeclick(); } /** * build , 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 dialog dialog.setcontentview(rootview);// set content view of dialog dialog.setcancelable(false);// set dialog 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 less or equal 0 stop handler { handler.removecallbacks(this);//remove queued post settime(defaulttime);// reset default time if(dialoglistener != null) dialoglistener.onnegativeclick();// simulate negative button click } else { buildui();// rebuild ui of dialog } } }; if (handler != null) { handler.postdelayed(runnable, 1000);// send post after 1 second = 1000 ms } }
}
in android activity create custome dialog view, , don't foget set dialog listener handle dialog click.
final mydialogview mydialogview = new mydialogview(mainactivity.this, "my dialog", "this 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(); } });
- result
Comments
Post a Comment