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()方法是线程安全的。
易探云服务器怎么过户/转让?易探云支持云服务器PUSH功能,该功能可将云服务器过户给指定用户。可带价PUSH,收到PUSH请求的用户在接收云服务器的同时,系统会扣除接收方的款项,同时扣除相关手续费,然后将款项打到发送方的账户下。易探云“PUSH服务器”的这一功能,可以让用户将闲置云服务器转让给更多需要购买的用户!易探云服务器怎么过户/PUSH?1.PUSH双方必须为认证用户:2.买家未接收前,卖家...
轻云互联成立于2018年的国人商家,广州轻云互联网络科技有限公司旗下品牌,主要从事VPS、虚拟主机等云计算产品业务,适合建站、新手上车的值得选择,香港三网直连(电信CN2GIA联通移动CN2直连);美国圣何塞(回程三网CN2GIA)线路,所有产品均采用KVM虚拟技术架构,高效售后保障,稳定多年,高性能可用,网络优质,为您的业务保驾护航。官方网站:点击进入广州轻云网络科技有限公司活动规则:1.用户购...
公司成立于2021年,专注为用户提供低价高性能云计算产品,致力于云计算应用的易用性开发,面向全球客户提供基于云计算的IT解决方案与客户服务,拥有丰富的国内BGP、三线高防、香港等优质的IDC资源。公司一直秉承”以人为本、客户为尊、永续创新”的价值观,坚持”以微笑收获友善, 以尊重收获理解,以责任收获支持,以谦卑收获成长”的行为观向客户提供全面优质的互...
dialoginterface为你推荐
声纹识别声纹识别有哪些领域的应用?联机侠解说:教大家怎么在我的世界联机换皮肤宝应中学江苏省宝应中学的知名校友草莓派怎么做草莓派?svn服务器搭建如何在本机搭建SVN服务器企业资源管理系统企业管理系统都有什么功能网络电话永久免费打有没有永久免费的网络电话鄂n鄂N的车牌是那里的?jstz举手望,草上马跑,打什么数字?assemblyinfoCSE和CS有什么区别
代理主机 m3型虚拟主机 备案域名购买 fc2新域名 汉邦高科域名申请 sugarhosts softlayer shopex空间 天猫双十一抢红包 网站实时监控 河南移动邮件系统 umax120 qq金券 cdn网站加速 netvigator 沈阳idc hosting24 云主机 租主机 国内免备案cdn 更多