dialoginterfacedialog.dismiss()和dialog.cancel()有什么区别分别用在什么场合
dialoginterface 时间:2021-06-03 阅读:(
)
alertDialog对话框的DialogInterface dialo参数有什么用
当你要通过确定或取消按钮来关闭对话框的时候,你就可以通过这个dialog对象(实际上就是设置监听事件的这个dialog)来对自身进行dismiss,后面那个int类型参数你查一下api就知道了android中带图标的按钮(ImageButton)怎么用
除了Android系统自带的Button按钮以外,还提供了带图标的按钮ImageButton
要制作带图标的按钮,首先要在布局文件中定义ImageButton,然后通过setImageDrawable方法来设置要显示的图标。
注意:
我们可以在布局文件中就直接设置按钮的图标,如
android:src=”@drawable/icon1″
我们也可以在程序中设置自定义图标
imgbtn3.setImageDrawable(getResources().getDrawable(R.drawable.icon2));
我们还可以使用系统自带的图标
imgbtn4.setImageDrawable(getResources().getDrawable(android.R.drawable.sym_call_ing));
设置完按钮的图标后,需要为按钮设置监听setOnClickListener,以此捕获事件并处理
下面的例子讲述的是由4个图标按钮组成的布局,其中三个按钮的图标是自定义的,第四个按钮的图标是系统的,当点击按钮1的时候,弹出dialog,当点击按钮2的时候,点击确定后,可以将按钮2的图标变成按钮3的图标,当点击按钮3的时候,按钮3的图标变成了系统打电话的图标,点击按钮4,显示一个提示dialog
ImageButtonTest.java源代码
.loulijun.imagebutton;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class ImageButtonTest extends Activity {
/** Called when the activity is first created. */
TextView iew;
ImageButton imgbtn1;
ImageButton imgbtn2;
ImageButton imgbtn3;
ImageButton imgbtn4;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iew=(TextView)findViewById(iew);
//分别取得4个ImageButton对象
imgbtn1=(ImageButton)findViewById(R.id.imagebutton1);
imgbtn2=(ImageButton)findViewById(R.id.imagebutton2);
imgbtn3=(ImageButton)findViewById(R.id.imagebutton3);
imgbtn4=(ImageButton)findViewById(R.id.imagebutton4);
//分别为ImageButton设置图标
//imgbtn1已经在main.xml布局中设置了图标,所以就不在这里设置了(设置图标即可在程序中设置,也可在布局文件中设置)
imgbtn2.setImageDrawable(getResources().getDrawable(R.drawable.icon));//在程序中设置图标
imgbtn3.setImageDrawable(getResources().getDrawable(R.drawable.icon2));
imgbtn4.setImageDrawable(getResources().getDrawable(android.R.drawable.sym_call_ing));//设置系统图标
//下面为各个按钮设置事件监听
imgbtn1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Dialog dialog=new AlertDialog.Builder(ImageButtonTest.this)
.setTitle("提示")
.setMessage("我是ImageButton1")
.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//相应的处理操作
}
}).create();
dialog.show();
}
});
imgbtn2.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Dialog dialog=new AlertDialog.Builder(ImageButtonTest.this)
.setTitle("提示")
.setMessage("我是ImageButton2,我要使用ImageButton3的图标")
.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
imgbtn2.setImageDrawable(getResources().getDrawable(R.drawable.icon2));
}
}).create();
dialog.show();
}
});
imgbtn3.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Dialog dialog=new AlertDialog.Builder(ImageButtonTest.this)
.setTitle("提示")
.setMessage("我是ImageButton3,我想使用系统打电话的图标")
.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
imgbtn3.setImageDrawable(getResources().getDrawable(android.R.drawable.sym_action_call));
}
}).create();
dialog.show();
}
});
imgbtn4.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Dialog dialog=new AlertDialog.Builder(ImageButtonTest.this)
.setTitle("提示")
.setMessage("我是使用的系统图标")
.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//相应的处理操作
}
}).create();
dialog.show();
}
});
}
}
布局文件main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/iew"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ImageButton测试案例"
/>
<ImageButton
android:id="@+id/imagebutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon1"
/>
<ImageButton
android:id="@+id/imagebutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageButton
android:id="@+id/imagebutton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageButton
android:id="@+id/imagebutton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>如何设置对话框的宽度和高度
AlertDialog.Builderdialog=newAlertDialog.Builder(this).setTitle(<br>"title").setIcon(android.R.drawable.ic_dialog_alert).setMessage(<br>"message").setPositiveButton("yes",newOnClickListener(){<br>publicvoidonClick(DialogInterfacearg0,intarg1){<br><br>}<br>}).setNegativeButton("no",<br>newOnClickListener(){<br>publicvoidonClick(DialogInterfacearg0,intarg1){<br>}<br>}).setCancelable(false);<br><br>/*方法1:<br>*将对话框的大小按屏幕大小的百分比设置<br>*/<br>WindowManagerm=getWindowManager();<br>Displayd=m.getDefaultDisplay();//获取屏幕宽、高用<br>WindowManager.LayoutParamsp=getWindow().getAttributes();//获取对话框当前的参数值<br>p.height=(int)(d.getHeight()*0.5);//高度设置为屏幕的0.5<br>p.width=(int)(d.getWidth()*0.8);//宽度设置为屏幕的0.8<br>dialog.show().getWindow().setAttributes(p);<br><br>/*方法2:<br>*获取对话框的窗口对象及参数对象以修改对话框的布局设置,<br>*可以直接调用getWindow(),表示获得这个Activity的Window<br>*对象,这样这可以以同样的方式改变这个Activity的属性.<br>*/<br>WindowdialogWindow=dialog.show().getWindow();<br>WindowManager.LayoutParamslp=dialogWindow.getAttributes();<br>dialogWindow.setGravity(CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);<br><br>/*<br>*lp.x与lp.y表示相对于原始位置的偏移.<br>*当参数值包含Gravity.LEFT时,对话框出现在左边,所以lp.x就表示相对左边的偏移,负值忽略.<br>*当参数值包含Gravity.RIGHT时,对话框出现在右边,所以lp.x就表示相对右边的偏移,负值忽略.<br>*当参数值包含Gravity.TOP时,对话框出现在上边,所以lp.y就表示相对上边的偏移,负值忽略.<br>*当参数值包含Gravity.BOTTOM时,对话框出现在下边,所以lp.y就表示相对下边的偏移,负值忽略.<br>*当参数值包含Gravity.CENTER_HORIZONTAL时<br>*,对话框水平居中,所以lp.x就表示在水平居中的位置移动lp.x像素,正值向右移动,负值向左移动.<br>*当参数值包含Gravity.CENTER_VERTICAL时<br>*,对话框垂直居中,所以lp.y就表示在垂直居中的位置移动lp.y像素,正值向右移动,负值向左移动.<br>*gravity的默认值为Gravity.CENTER,即Gravity.CENTER_HORIZONTAL|<br>*Gravity.CENTER_VERTICAL.<br>*<br>*本来setGravity的参数值为Gravity.LEFT|Gravity.TOP时对话框应出现在程序的左上角,但在<br>*我手机上测试时发现距左边与上边都有一小段距离,而且垂直坐标把程序标题栏也计算在内了,<br>*Gravity.LEFT,Gravity.TOP,Gravity.BOTTOM与Gravity.RIGHT都是如此,据边界有一小段距离<br>*/<br>lp.x=100;//新位置X坐标<br>lp.y=100;//新位置Y坐标<br>lp.width=300;//宽度<br>lp.height=300;//高度<br>lp.alpha=0.7f;//透明度<br><br>//当Window的Attributes改变时系统会调用此函数,可以直接调用以应用上面对窗口参数的更改,也可以用setAttributes<br>//dialog.onWindowAttributesChanged(lp);<br>dialogWindow.setAttributes(lp);Android中添加事件监听器的方法有哪些?
很简单:
myButton1.setOnClickListener(newButton.OnClickListener(){?
????????@Override?
????????public?voidonClick(View?v)?{?
????????}????????????
});android 开发 怎么给dialog 中添加动画
AlertDialog控件类是可以添加按钮,标题等
如:
AlertDialog ale = new AlertDialog.Builder(MainActivity.this).create();
ale.setTitle("操作提示");
ale.setMessage("绑定设备完成");
ale.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
ale.show();dialog.dismiss()和dialog.cancel()有什么区别分别用在什么场合
他们两个都基本上一样的,public void cancel ()Cancel the dialog. This is essentially the same as calling dismiss(), but it will also call your DialogInterface.OnCancelListener (if registered). 这个是解释,dismiss()方法是线程安全的。
介绍:御速云成立于2021年的国人商家,深圳市御速信息技术有限公司旗下品牌,为您提供安全可靠的弹性计算服务,随着业务需求的变化,您可以实时扩展或缩减计算资源,使用弹性云计算可以极大降低您的软硬件采购成本,简化IT运维工作。主要从事VPS、虚拟主机、CDN等云计算产品业务,适合建站、新手上车的值得选择,拥有华东江苏、华东山东等国内优质云产品;香港三网直连(电信CN2GIA联通移动CN2直连);美国高...
公司介绍成都随风云科技有限公司成立于2021年,是国内领先的互联网业务平台服务提供商。公司专注为用户提供低价高性能云计算产品,致力于云计算应用的易用性开发,并引导云计算在国内普及。目前公司研发以及运营云服务基础设施服务平台(IaaS),面向全球客户提供基于云计算的IT解决方案与客户服务,拥有丰富的国内BGP、双线高防、香港等优质的IDC资源。公司一直秉承”以人为本、客户为尊、永续创新&...
Hostiger商家我们可能以前也是有见过的,以前他们的域名是Hostigger,后来进行微调后包装成现在的。而且推出Columbus Day哥伦布日优惠活动,提供全场的VPS主机首月7折月付2.79美元起的优惠。这里我们普及一下基础知识,Columbus Day ,即为每年10月12日,是一些美洲国家的节日,纪念克里斯托弗·哥伦布在北美登陆,为美国的联邦假日。Hostiger 商家是一个成立于2...
dialoginterface为你推荐
tde如何区别动作数据与模型的种类。tde tda lat 啊? mmdsvn服务器搭建如何在本机搭建SVN服务器orphanremoval我的电脑开机时自检,出现许多这样的字样:Deleting orphan file record segment XXXX (XXXX代表数字)。腾讯年终奖腾讯工作怎么样印度尼西亚国家代码谁知道世界各国的国家电话代码?jstz举手望,草上马跑,打什么数字?云计划什么是云查杀,云计算和云计划的关系?单元测试规范单元测试用例该怎么写超级播放器推荐个好的视频播放器文本框透明html怎么把这个文本框变为透明?
北京vps cn域名个人注册 512m windows2003iso 华为4核 中国电信测速112 秒杀预告 网站木马检测工具 isp服务商 qq云端 服务器合租 银盘服务 韩国代理ip 日本代理ip fatcow reboot server2008 cc加速器 vi命令 web服务器搭建软件 更多