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()方法是线程安全的。
Megalayer是新晋崛起的国外服务器商,成立于2019年,一直都处于稳定发展的状态,机房目前有美国机房,香港机房,菲律宾机房。其中圣何塞包括CN2或者国际线路,Megalayer商家提供了一些VPS特价套餐,譬如15M带宽CN2线路主机最低每月48元起,基于KVM架构,支持windows或者Linux操作系统。。Megalayer技术团队行业经验丰富,分别来自于蓝汛、IBM等知名企业。Mega...
RAKsmart 虽然是美国主机商,但是商家的主要客户群还是在我们国内,于是我们可以看到每次的国内节日促销活动期间商家也会发布促销。包括这次年中大促活动,RAKsmart商家也有发布为期两个月的年终活动,其中有商家擅长的独立服务器和便宜VPS主机。服务器包括站群服务器、特价服务器、高达10G带宽不限制流量的美国服务器。商家优惠活动,可以看到对应商品的优惠,同时也可以使用 优惠码 RAKBL9 同时...
Sharktech荷兰10G带宽的独立服务器月付319美元起,10Gbps共享带宽,不限制流量,自带5个IPv4,免费60Gbps的 DDoS防御,可加到100G防御。CPU内存HDD价格购买地址E3-1270v216G2T$319/月链接E3-1270v516G2T$329/月链接2*E5-2670v232G2T$389/月链接2*E5-2678v364G2T$409/月链接这里我们需要注意,默...
dialoginterface为你推荐
请求超时DNS请求超时,怎么办?mac地址克隆无线路由器mac地址克隆有什么用处?模式识别算法模式识别、神经网络、遗传算法、蚁群算法等等人工智能算法需要哪些数学知识?部署工具如何使用office2016部署软件qq网络硬盘怎么用qq网络硬盘天翼校园宽带天翼校园宽带 是怎么算时间的 一个月 是指从办理那天开始 往后 30天是一个月吗 还是 办理的那天所在的那个超级播放器一共有哪些播放器?移动硬盘文件或目录损坏且无法读取移动硬盘文件或目录损坏且无法读取怎么办??电子邮件软件邮件客户端软件上传图片网站速度超快的上传图片网站
解析域名 网站域名备案 免费域名申请 vultr美国与日本 mach 搜狗抢票助手 ev证书 一点优惠网 免费ftp空间申请 太原联通测速平台 七夕促销 泉州电信 静态空间 可外链网盘 1g内存 raid10 宏讯 www789 starry 免费ftp 更多