Android 带有默认按钮倒计时的对话框按钮
如何为用户创建对话框,而不是常规对话框, 存在按钮\按钮,其中一个按钮将作为默认答案, 这个按钮会准时倒计时 例如: 对话:“你好,我可以…………?(是/否) 秒数后,将自动接受No 这是另一个示例: 像这样:Android 带有默认按钮倒计时的对话框按钮,android,jquery,dialog,android-alertdialog,countdowntimer,Android,Jquery,Dialog,Android Alertdialog,Countdowntimer,如何为用户创建对话框,而不是常规对话框, 存在按钮\按钮,其中一个按钮将作为默认答案, 这个按钮会准时倒计时 例如: 对话:“你好,我可以…………?(是/否) 秒数后,将自动接受No 这是另一个示例: 像这样: 这可能是一个完美的答案,但我认为它对您很有用,您可以根据需要自定义代码 创建一个具有dailog自定义视图的布局,将其命名为(my_dialog_layout.xml): <?xml version="1.0" encoding="utf-8"?> <Linea
这可能是一个完美的答案,但我认为它对您很有用,您可以根据需要自定义代码 创建一个具有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;
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
}
}
-结果
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
}
}
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();
}
});