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()方法是线程安全的。
tmhhost怎么样?tmhhost正在搞暑假大促销活动,全部是高端线路VPS,现在直接季付8折优惠,活动截止时间是8月31日。可选机房及线路有美国洛杉矶cn2 gia+200G高防、洛杉矶三网CN2 GIA、洛杉矶CERA机房CN2 GIA,日本软银(100M带宽)、香港BGP直连200M带宽、香港三网CN2 GIA、韩国双向CN2。点击进入:tmhhost官方网站地址tmhhost优惠码:Tm...
Megalayer 商家在开始看到有提供香港服务器、香港站群服务器的时候有介绍过,后来就一直没有怎么关注。但是前几天有看到网友使用到他们家的美国独立服务器问其如何的,但是我没有使用过就不好评论,这不前几天也有介绍到Megalayer美国独立服务器。以及我们也有看到商家有提供美国站群服务器和美国大带宽服务器产品,可选30M不限制流量CN2优化线路,以及100M不限制流量国际带宽线路。新年元旦后,Me...
专心做抗投诉服务器的VirtVPS上线瑞士机房,看中的就是瑞士对隐私的保护,有需要欧洲抗投诉VPS的朋友不要错过了。VirtVPS这次上新的瑞士服务器采用E-2276G处理器,Windows/Linux操作系统可选。VirtVPS成立于2018年,主营荷兰、芬兰、德国、英国机房的离岸虚拟主机托管、VPS、独立服务器、游戏服务器和外汇服务器业务。VirtVPS 提供世界上最全面的安全、完全受保护和私...
dialoginterface为你推荐
决策树分析简述决策树方法的基本内涵病历单我想请两天病假,病例单怎么写以图搜人怎样人肉搜人?只知道他名字和当初居住的地址virusscanvirus scan 是个什么软件?qq博客怎么开QQ博客啊!华为总裁女儿为啥姓孟任正非女儿为什么姓孟?腾讯技术腾讯是什么东西?asp大马黑帽seo的webshell中,什么是大马和小马assemblyinfoCSE和CS有什么区别谷歌图片识别怎么通过一张GIF图在网上搜索出其出处(你们懂的...)以图搜图那个百度只找到了一模一样的..,有
vps安全设置 北京vps主机 二级域名申请 万网域名解析 主机测评 骨干网 80vps oneasiahost 私人服务器 优惠码 idc测评网 美国便宜货网站 表单样式 info域名 好看的桌面背景图 国外php空间 mysql主机 免费个人网站申请 cdn联盟 腾讯实名认证中心 更多