listviewitem上未显示警报框单击android
我想在alertbox中设置文本视图文本。我想做倒计时。我的代码如下: 我的自定义警报框listviewitem上未显示警报框单击android,android,textview,Android,Textview,我想在alertbox中设置文本视图文本。我想做倒计时。我的代码如下: 我的自定义警报框 public abstract class ChallengeDialog extends AlertDialog.Builder implements OnClickListener { TextView msg; /** * @param context * @param title resource id * @param message resource id */
public abstract class ChallengeDialog extends AlertDialog.Builder implements OnClickListener {
TextView msg;
/**
* @param context
* @param title resource id
* @param message resource id
*/
public ChallengeDialog(Context context, String title, String message) {
super(context);
setTitle(title);
// setMessage(message);
msg = new TextView(context);
msg.setText(message);
setView(msg);
setPositiveButton("accept", this); //In android this is OK button
setNegativeButton("reject", this);//In android this is cancel button
}
public void setDialogMsg(String m){
msg.setText(m);
setView(msg);
}
/**
* will be called when "cancel" pressed.
* closes the dialog.
* can be overridden.
* @param dialog
*/
//This is cancel button
public void onOkClicked(DialogInterface dialog) {
dialog.dismiss();
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
dialog.dismiss();
} else {
onOkClicked(dialog);
}
}
/**
* called when "ok" pressed.
* @param input
* @return true, if the dialog should be closed. false, if not.
*/
//This is challenge button
abstract public boolean onCancelClicked(String input);
}
使用自定义警报框的代码
ChallengeDialog chlngDialog = new ChallengeDialog(MyActivity.this,"title","count") {
@Override
public boolean onCancelClicked(String input) {
// TODO Auto-generated method stub
return false;
}
};
AlertDialog a = chlngDialog.show();
for(int i = 15 ; i>0 ;i--){
try {
chlngDialog.setDialogMsg("count "+i);
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//a.dismiss();
我正在单击listview执行此代码。但警报框在15秒后显示,我点击了listview项。我不知道为什么会这样 您是否将for循环放入了
onItemClick
listener
如果是,则创建一个异步任务并在
onItemClick
listener中执行该任务。将for循环放入doInBackground
您是否将for循环放入onItemClick
侦听器
如果是,则创建一个异步任务并在
onItemClick
listener中执行该任务。将for循环放入doInBackground
正如Android Coader建议的那样,我已经完成了异步任务。代码如下:
public abstract class MyDialog extends AlertDialog.Builder implements OnClickListener {
TextView msgTxt;
private static int count= 15 ;
private String user;
private int c;
/**
* @param context
* @param title resource id
* @param message resource id
*/
public ChallengeDialog(Context context, String title, String message) {
super(context);
setTitle(title);
setCancelable(false);
// setMessage(message);
msgTxt = new TextView(context);
msgTxt.setText(message);
setView(msgTxt);
setPositiveButton("accept", this); //In android this is OK button
setNegativeButton("reject", this);//In android this is cancel button
MyTimerTask myTask = new MyTimerTask();
myTask.execute(new String[] { "abc" });
}
public void setUserAndBet(String u,int b){
c= b;
user = u ;
}
/**
* will be called when "cancel" pressed.
* closes the dialog.
* can be overridden.
* @param dialog
*/
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
onAcceptClicked(dialog);
// dialog.dismiss();
} else {
onRejectClicked(dialog);
}
}
/**
* called when "ok" pressed.
* @param input
* @return true, if the dialog should be closed. false, if not.
*/
//This is accept button
abstract public boolean onAcceptClicked(DialogInterface d);
abstract public boolean onRejectClicked(DialogInterface d);
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
int countDownValue = (Integer)msg.obj;
msgTxt.setText(countDownValue +" seconds");
//call setText here
}
};
class MyTimerTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... urls) {
//mHandler.sendEmptyMessage(0);
while(count > 0 ){
Message msg = new Message();
int countDownValue = count--;
msg.obj = countDownValue;
mHandler.sendMessage(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return " ";
}
}
}
公共抽象类MyDialog扩展了AlertDialog.Builder实现了OnClickListener{
文本视图msgTxt;
私有静态整数计数=15;
私有字符串用户;
私人INTC;
/**
*@param上下文
*@param title资源id
*@param消息资源id
*/
公共ChallengeDialog(上下文、字符串标题、字符串消息){
超级(上下文);
片名(片名);
可设置可取消(假);
//设置消息(消息);
msgTxt=新文本视图(上下文);
msgTxt.setText(消息);
setView(msgTxt);
setPositiveButton(“接受”,this);//在android中,这是OK按钮
setNegativeButton(“拒绝”,this);//在android中,这是取消按钮
MyTimerTask myTask=新建MyTimerTask();
execute(新字符串[]{“abc”});
}
公共void setUserAndBet(字符串u,int b){
c=b;
用户=u;
}
/**
*将在按下“取消”时调用。
*关闭对话框。
*可以重写。
*@param对话框
*/
@凌驾
public void onClick(DialogInterface dialog,int which){
如果(which==DialogInterface.BUTTON_正数){
onAcceptClicked(对话框);
//dialog.dismise();
}否则{
点击(对话框);
}
}
/**
*按下“ok”时调用。
*@param输入
*@return true,如果对话框应该关闭,则返回true。否则返回false。
*/
//这是“接受”按钮
抽象公共布尔值onAcceptClicked(Dialogd接口);
抽象公共布尔值(DialogD接口);
Handler mHandler=新处理程序(){
@凌驾
公共无效handleMessage(消息消息消息){
int countDownValue=(整数)msg.obj;
msgTxt.setText(倒计时值+秒);
//在这里呼叫setText
}
};
类MyTimerTask扩展了异步任务{
@凌驾
受保护的字符串doInBackground(字符串…URL){
//mHandler.sendEmptyMessage(0);
而(计数>0){
Message msg=新消息();
int countDownValue=计数--;
msg.obj=倒计时值;
mHandler.sendMessage(msg);
试一试{
睡眠(1000);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
返回“”;
}
}
}
只需创建此调用的对象并调用show()方法。感谢Adndroid Coader我完成了Android Coader建议的异步任务。代码如下:
public abstract class MyDialog extends AlertDialog.Builder implements OnClickListener {
TextView msgTxt;
private static int count= 15 ;
private String user;
private int c;
/**
* @param context
* @param title resource id
* @param message resource id
*/
public ChallengeDialog(Context context, String title, String message) {
super(context);
setTitle(title);
setCancelable(false);
// setMessage(message);
msgTxt = new TextView(context);
msgTxt.setText(message);
setView(msgTxt);
setPositiveButton("accept", this); //In android this is OK button
setNegativeButton("reject", this);//In android this is cancel button
MyTimerTask myTask = new MyTimerTask();
myTask.execute(new String[] { "abc" });
}
public void setUserAndBet(String u,int b){
c= b;
user = u ;
}
/**
* will be called when "cancel" pressed.
* closes the dialog.
* can be overridden.
* @param dialog
*/
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
onAcceptClicked(dialog);
// dialog.dismiss();
} else {
onRejectClicked(dialog);
}
}
/**
* called when "ok" pressed.
* @param input
* @return true, if the dialog should be closed. false, if not.
*/
//This is accept button
abstract public boolean onAcceptClicked(DialogInterface d);
abstract public boolean onRejectClicked(DialogInterface d);
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
int countDownValue = (Integer)msg.obj;
msgTxt.setText(countDownValue +" seconds");
//call setText here
}
};
class MyTimerTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... urls) {
//mHandler.sendEmptyMessage(0);
while(count > 0 ){
Message msg = new Message();
int countDownValue = count--;
msg.obj = countDownValue;
mHandler.sendMessage(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return " ";
}
}
}
公共抽象类MyDialog扩展了AlertDialog.Builder实现了OnClickListener{
文本视图msgTxt;
私有静态整数计数=15;
私有字符串用户;
私人INTC;
/**
*@param上下文
*@param title资源id
*@param消息资源id
*/
公共ChallengeDialog(上下文、字符串标题、字符串消息){
超级(上下文);
片名(片名);
可设置可取消(假);
//设置消息(消息);
msgTxt=新文本视图(上下文);
msgTxt.setText(消息);
setView(msgTxt);
setPositiveButton(“接受”,this);//在android中,这是OK按钮
setNegativeButton(“拒绝”,this);//在android中,这是取消按钮
MyTimerTask myTask=新建MyTimerTask();
execute(新字符串[]{“abc”});
}
公共void setUserAndBet(字符串u,int b){
c=b;
用户=u;
}
/**
*将在按下“取消”时调用。
*关闭对话框。
*可以重写。
*@param对话框
*/
@凌驾
public void onClick(DialogInterface dialog,int which){
如果(which==DialogInterface.BUTTON_正数){
onAcceptClicked(对话框);
//dialog.dismise();
}否则{
点击(对话框);
}
}
/**
*按下“ok”时调用。
*@param输入
*@return true,如果对话框应该关闭,则返回true。否则返回false。
*/
//这是“接受”按钮
抽象公共布尔值onAcceptClicked(Dialogd接口);
抽象公共布尔值(DialogD接口);
Handler mHandler=新处理程序(){
@凌驾
公共无效handleMessage(消息消息消息){
int countDownValue=(整数)msg.obj;
msgTxt.setText(倒计时值+秒);
//在这里呼叫setText
}
};
类MyTimerTask扩展了异步任务{
@凌驾
受保护的字符串doInBackground(字符串…URL){
//mHandler.sendEmptyMessage(0);
而(计数>0){
Message msg=新消息();
int countDownValue=计数--;
msg.obj=倒计时值;
mHandler.sendMessage(msg);
试一试{
睡眠(1000);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
返回“”;
}
}
}
只需创建此调用的对象并调用show()方法。谢谢Adndroid Coader