Android怎么Toast出删除的值,Android设置Toast长时间显示,及AlertDialog的使用

本文介绍了如何在Android中解决Toast显示时间过短的问题,通过结合Timer类实现Toast的长时间显示。同时,建议在需要更多用户交互的情况下使用Dialog,提供了创建简单和选择性Dialog的示例代码,展示了Dialog在增强用户体验方面的优势。
摘要由CSDN通过智能技术生成

来源:

首先简单介绍一下Toast(吐司)。这个控件的用词相当准确,它所表现出来的特征正是吐司似的弹出效果,而且非常的简单便捷。Android自带的整个Toast功能能够帮助我们编写程序的时候实现简单的提醒功能,使得程序的应用变得更加友好。

可是在使用Toast的时候,无论是使用makeText(),还是使用toast.setDuration(),在其中进行Toast显示时间的设置时,取值Toast.LENGTH_LONG,或者是Toast.LENGTH_SHORT,Toast都会只是一闪而过,显示的时间很短。如果我们需要提醒大量的信息的时候,显然这样是用Toast是很不友好的。那么,如何能够使得Toast可以长时间显示呢?

在Java中有一个类,叫做Timer,这个类可以帮我们。

示例如下:

private Toast toast = null;

private class IntroButtonListener implements OnClickListener {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

toast = Toast.makeText(StartPage.this, toastText,Toast.LENGTH_LONG);

initToast();

execToast();

}

}

private void execToast(){

Timer timer = new Timer();

timer.schedule(new TimerTask(){

@Override

public void run() {

// TODO Auto-generated method stub

initToast();

}

}, 30);

}

private void initToast(){

toast.show();

}

我在这里将Toast的示例化延迟到了触发事件是再响应,然后调用的是makeText()方法,给它设置Duration的值为Toast.LENGTH_LONG,然后将toast.show()用一个方法initToast()包起来,这么做的目的是让它显示在Timer中也会接着显示我的Toast。

这么做的逻辑是,首先先显示Toast,然后让Timer帮助再次显示Toast,这样就会出现了Toast长时间显示的效果,如果想让时间变得更长,可以修改Timer里面timer.schedule()的值,我这里设置的是30。

你可以直接Copy这段代码,这是我测试通过的。如果你真的有必要利用提醒和用户产生互动,建议不要使用Toast,可以改用Dialog,可能效果会更好些。

Dialog dialog = new AlertDialog.Builder(StartPage.this)

.setTitle("提示")

.setMessage(toastText)

.setPositiveButton("确定", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog,int whichButton){

dialog.cancel();

}

}).create();

dialog.show();

这种对话框是只显示确定的对话框,也就是提示对话框,我们可以根据这个对话框写法的构造,给它添加成可以有选择功能的对话框:

Dialog dialog = new AlertDialog.Builder(ContactsSettingPage.this).setTitle(

"提示").setMessage("确定删除此项?").setPositiveButton("确定",

new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog,

int whichButton) {

//需要做的事情.....

}

}).setNegativeButton("取消",

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

// TODO Auto-generated method stub

dialog.cancel();

}

}).create();

dialog.show();

显然红色字体是我添加的内容,也就是多出来了一个“取消”按钮,这样就可以进行对话框的选择了。如果你想在确定之后做些什么,那就在蓝色的地方添加需要做的事情就好了。这样扩展对话框正好是设计模式中的装饰者模式,这样的设计具有非常便利的好处,面向对象的优越性跃然而出!

根据这样的推断,如果你还想添加其他神马按钮,不妨可以模仿试一试~

以下代码请忽略:

ConnectivityManager connMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

if (connMgr.getNetworkInfo(0).isConnected()) {

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setMessage("你现在使用的不是电信提供的Wifi接入点,继续访问电信相关业务可能会失败,是否继续?")

.setCancelable(false)

.setPositiveButton("继续", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {

dialog.cancel();

Intent i = new Intent();

i.setData(Uri.parse("http://www.baidu.com"));

startActivity(i);

ENet.this.finish();

}

})

.setNegativeButton("退出", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {

ENet.this.finish();

}

});

AlertDialog alert = builder.create();

alert.show();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值