AlertDialog相关学习

来源:互联网 发布:mac数位板驱动怎么安装 编辑:程序博客网 时间:2024/05/22 02:03

(1)AlertDialog学习总结

常用方法:

setTitle

setMessage

setCancelable

setPositiveButton

setNegativeButton

可以把AlertDialog理解成三部分,title,content,button

title也就是标题:对应的方法有setTitle(Stringtitle),setCustomTitle(View view)setIcon.

setTitle比较容易,设置好文字内容。但是title默认竖直居中,靠左。无法修改,并且字体颜色,字体大小不可调。(至少我不知道)

setCustomTitle就比较自由了,自定义的。可以在代码中TextViewmTextView=new TextView(this);

也可以在res/layout下整个布局,新建个TextView.

然后LayoutInflater mlay=LayoutInflater.from(this);

ViewmView=mlay.inflate(R.layout.xxx);

TextViewmTextView=(TextView)mView.findViewById(R.id.my_textview);//注意要加上mView

setCustomTitle(mTextView);

content就是中间的部分:对应的方法有setMessage,setItems(相当于一个ListView)...

比较重要的是setView(Viewview),可以自定义布局,然后自己按上面的方式获取到组件的对象后自己添加点击等事件。

button部分:对应的方法有setPositiveButtonsetNegativeButton,setNeutralButton暂时还没详细调查能否自定义这一块。

dialog最后要记得dialog.show()

(2)如果想把AlertDialog推翻重来怎么写

AlertDialog myAlertDialog=newAlertDialog.Builder(MainActivity.this).create();

myAlertDialog.show();

myAlertDialog.getWindow().setContentView(R.layout.activity_main);

这样不会引用AlertDialog.Buider的三部分结构,而是完全使用你自定义的View。

(3)AlertDialog的各种监听

dialog.setPositiveButton(text,listener) //点击positivebtn时触发

dialog.setNegativeButton(text,listener) //点击negativebtn时触发

dialog.setOnCancelListener(onCancelListener)//只有在点击手机的返回键的时候会触发

dialog.setOnDismissListener(onDismissListener)//关闭dialog的时候触发。

包括手机点击返回键,点击positivebtn,negativebtn,点击对话框外区域导致对话框消失

(4)自定义AlertDialog

在这里我们完全不考虑AlertDialog.Builder类的使用,因为AlertDialog.Builder的位置不可控。(起码我不知道)

目的是AlertDialog布局完全重写,可以控制AlertDialog在屏幕中任意位置。

自己尝试后,有两种写法。

注意:两种写法,对话框最大宽度都不能达到屏幕宽度。始终会有一点间距,左右各8dp左右。

第一种写法比较完美:

WindowManager.LayoutParams类相关参数修改后,可生效。

Res/layoutxml文件中涉及到布局的高宽,margin也能生效。

左右margin如果设置为5dp,那么实际是13dp左右,因为加上了本来的间距。

android:width,android:height设置的是什么就是什么。

第二种写法:

WindowManager.LayoutParams类相关参数修改后,可生效。

Res/layoutxml文件中涉及到布局的宽度,margin不能生效。高度两种情况都是生效,和内容大小一致。无论你怎么设置。android:width=”match_parent”android:height=”wrap_content”


第一种写法代码

AlertDialog myAlertDialog = new AlertDialog.Builder(this).create();

Window w = myAlertDialog.getWindow();

WindowManager.LayoutParamslp = w.getAttributes();

lp.y=200;

lp.gravity=Gravity.BOTTOM;

myAlertDialog.show();

//w.setLayout(333,333);

w.setAttributes(lp);

myAlertDialog.getWindow().setContentView(R.layout.fuck);

Button cancelButton = (Button) myAlertDialog.findViewById(R.id.cancel_btn);

Button commitButton = (Button) myAlertDialog.findViewById(R.id.commit_btn);

第二种写法代码:

AlertDialog myAlertDialog = new AlertDialog.Builder(this).create();

Windoww = myAlertDialog.getWindow();

WindowManager.LayoutParamslp = w.getAttributes();

lp.y=200;

lp.gravity=Gravity.BOTTOM;

myAlertDialog.show();

//w.setLayout(333,333);

w.setAttributes(lp);

LayoutInflater mInflater = LayoutInflater.from(this);

ViewmView = mInflater.inflate(R.layout.fuck,null);

Button cancelButton = (Button) mView.findViewById(R.id.cancel_btn);

Button commitButton = (Button) mView.findViewById(R.id.commit_btn);

myAlertDialog.getWindow().setContentView(mView);


注意点:

1.w.setLayout(333,333)

myAlertDialog.getWindow().setContentView();

这两行都要放在myAlertDialog.show();之后,否则不生效。

2.AlertDialog继承于Dialog。原来除了View类和Activity类外Dialog类也有findViewById方法。

AlertDialog如果在触摸对话框边缘外部,对话框消失。使用下面任意一个方法都可解决。

myAlertDialog.setCancelable(false);

myAlertDialog.setCanceledOnTouchOutside(false);

区别在于

setCanceledOnTouchOutside(false);调用这个方法时,按对话框以外的地方不起作用。按返回键会关对话框。

setCanceleable(false);调用这个方法时,按对话框以外的地方不起作用。按返回键也不会关对话框。

(4)控件PopupWindow类学习总结

importandroid.widget.PopupWindow;

PopupWindowAlertDialog最终的目的都是为了实现一个对话框。

我们需要达到的目的有:

1.自定义View(对话框大小可自己随意设计)

2.对话框位置可以随意放置。

3.对话框的动作控制。分几种情况

AlertDialog好控制

点击对话框区域外,对话框消失。点击back,对话框消失。

myAlertDialog.setCanceledOnTouchOutside(true);

点击对话框区域外,对话框不消失。点击back,对话框消失。myAlertDialog.setCanceledOnTouchOutside(false);

点击对话框区域外,对话框不消失,点击back,对话框不消失。

myAlertDialog.setCancelable(false);

PopupWindow不好控制

setOutsideTouchablePopupWindow中貌似一点用都没有。

点击对话框区域外,对话框消失。点击back,对话框消失。

mPopupWindow.setFocusable(true);

mPopupWindow.setBackgroundDrawable(newColorDrawable(0x00000000));//设置透明背景

点击对话框区域外,对话框不消失。点击back,对话框消失。

不知道怎么搞

网上的说法(未实践):

LayoutInflaterinflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

Viewcontentview = inflater.inflate(R.layout.popup_process, null);

contentview.setFocusable(true);//这个很重要

contentview.setFocusableInTouchMode(true);

finalPopupWindow popupWindow = newPopupWindow(contentview,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

popupWindow.setFocusable(true);

popupWindow.setOutsideTouchable(false);

contentview.setOnKeyListener(newOnKeyListener() {

@Override

public boolean onKey(View v, int keyCode, KeyEvent event) {

if(keyCode == KeyEvent.KEYCODE_BACK) {

popupWindow.dismiss();

return true;

}

return false;

}

});

点击对话框区域外,对话框不消失,点击back,对话框不消失。

mPopupWindow.setFocusable(true);

PopupWindow基本用法

自定义View

View mView=getLayoutInflater().inflate(R.layout.popup, null);

mPopupWindow=newPopupWindow(mView, LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, true);

mPopupWindow.setBackgroundDrawable(newColorDrawable(0x00000000));

mButton.setOnClickListener(newOnClickListener() {

public void onClick(View v) {

//TODO Auto-generated method stub

mPopupWindow.showAsDropDown(v);

}

});

mButton2.setOnClickListener(newOnClickListener() {

public void onClick(View v) {

//TODO Auto-generated method stub

mPopupWindow.dismiss();

}

});

对话框位置可以随意放置

showAsDropDown(Viewanchor):相对某个控件的位置(正左下方),无偏移

showAsDropDown(Viewanchor, int xoff, int yoff):相对某个控件的位置,有偏移

showAtLocation(Viewparent, int gravity, int x, inty)相对于父控件(准确的说就是当前ActivitysetContentView的根布局)的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移。

showAtLocationparent参数可以很随意,只要是activity中的view都可以。

PopupWindow的优势在于还可以设置动画

mPopupWindow.setAnimationStyle(R.style.anim_menu_bottombar);

AlertDialogAlertDialog.Builderapi中没找到setAnimation类似的方法。

res/value/styles.xml添加一个style

<stylename="anim_menu_bottombar">

<itemname="android:windowEnterAnimation">@anim/menu_bottombar_in</item>

<itemname="android:windowExitAnimation">@anim/menu_bottombar_out</item>

</style>

在工程res下新建anim文件夹,在anim文件夹先新建两个xml文件

menu_bottombar_in.xml

<?xmlversion="1.0" encoding="utf-8"?>

<setxmlns:android="http://schemas.android.com/apk/res/android">

<translate

android:duration="250"

android:fromYDelta="100.0%"//相当于控件的高度

android:toYDelta="0.0"/>

</set>

menu_bottombar_out.xml

<?xmlversion="1.0" encoding="utf-8"?>

<setxmlns:android="http://schemas.android.com/apk/res/android">

<translate

android:duration="250"

android:fromYDelta="0.0"

android:toYDelta="100%"/>

</set>

(5)AlertDialog主题设置

AlertDialog.BuildermBuilder = newAlertDialog.Builder(AlarmClockSettingActivity.this,AlertDialog.THEME_HOLO_LIGHT);

publicstatic final int THEME_TRADITIONAL = 1;

publicstatic final int THEME_HOLO_DARK = 2;

publicstatic final int THEME_HOLO_LIGHT = 3;

publicstatic final int THEME_DEVICE_DEFAULT_DARK = 4;

publicstatic final int THEME_DEVICE_DEFAULT_LIGHT = 5;

默认是THEME_TRADITIONAL。但是不同版本sdk或者手机对应的默认值不一样,会导致效果不一样看起来。

所以得统一下主题。

对于TimePicker也是一样的,不同sdk效果也差别很大。所以在控件的组件属性中设置style.例如:

<TimePicker

android:id="@+id/my_timepicker"

android:theme="@android:style/Theme.Holo.Light"

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

原创粉丝点击