Android默认的PopupWindow和EditText的外观是矩形框,看起来不是太好,本示例通过设置布局View的背景和PopupWindowd对象的背景,实现有白色圆角边框的对话框效果和圆角文字编辑框。代码如下(关键部分是背景布局XML):
对话框弹出效果图:
- package com.test;
- import android.app.Activity;
- import android.content.Context;
- import android.os.Bundle;
- import android.text.InputType;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.PopupWindow;
- import android.widget.LinearLayout.LayoutParams;
- public class RoundCorner extends Activity {
- Button mButton;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super .onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // 定义按钮
- mButton = (Button) this .findViewById(R.id.Button01);
- mButton.setOnClickListener( new ClickEvent());
- // 两个圆角文字编辑框
- EditText et1 = (EditText) this .findViewById(R.id.roundedtext1);
- EditText et2 = (EditText) this .findViewById(R.id.roundedtext2);
- et1.setInputType(InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);
- et2.setInputType(InputType.TYPE_NULL); //不显示软键盘
- }
- // 处理按键事件
- class ClickEvent implements OnClickListener {
- @Override
- public void onClick(View v) {
- if (v == mButton) {
- showRoundCornerDialog(RoundCorner. this , RoundCorner. this .findViewById(R.id.Button01));
- }
- }
- }
- // 显示圆角对话框
- public void showRoundCornerDialog(Context context, View parent) {
- LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- // 获取圆角对话框布局View,背景设为圆角
- final View dialogView = inflater.inflate(R.layout.popupwindow, null , false );
- dialogView.setBackgroundResource(R.drawable.rounded_corners_view);
- // 创建弹出对话框,设置弹出对话框的背景为圆角
- final PopupWindow pw = new PopupWindow(dialogView, 300 , LayoutParams.WRAP_CONTENT, true );
- pw.setBackgroundDrawable(getResources().getDrawable(R.drawable.rounded_corners_pop));
- //注:上面的设背景操作为重点部分,可以自行注释掉其中一个或两个设背景操作,查看对话框效果
- //注:上面的设背景操作为重点部分,可以自行注释掉其中一个或两个设背景操作,查看对话框效果
- final EditText edtUsername = (EditText) dialogView.findViewById(R.id.username_edit);
- final EditText edtPassword = (EditText) dialogView.findViewById(R.id.password_edit);
- edtUsername.setHint( "用户名..." ); // 设置提示语
- edtPassword.setHint( "密码..." ); // 设置提示语
- // OK按钮及其处理事件
- Button btnOK = (Button) dialogView.findViewById(R.id.BtnOK);
- btnOK.setOnClickListener( new OnClickListener() {
- @Override
- public void onClick(View v) {
- // 设置文本框内容
- edtUsername.setText( "username" );
- edtPassword.setText( "password" );
- }
- });
- // Cancel按钮及其处理事件
- Button btnCancel = (Button) dialogView.findViewById(R.id.BtnCancel);
- btnCancel.setOnClickListener( new OnClickListener() {
- @Override
- public void onClick(View v) {
- pw.dismiss(); // 关闭
- }
- });
- // 显示RoundCorner对话框
- pw.showAtLocation(parent, Gravity.CENTER|Gravity.BOTTOM, 0 , 0 );
- }
- }
1,圆角对话框的背景布局文件XML。
--------rounded_corners_pop.xml此为PopupWindow的背景布局文件
- <?xml version= "1.0" encoding= "utf-8" ?>
- <shape xmlns:android= "http://schemas.android.com/apk/res/android" >
- <solid android:color= "#ffffffff" />
- <stroke android:width= "3dp" color= "#ffff8080" />
- <corners android:radius= "10dp" />
- <padding android:left= "3dp" android:top= "3dp"
- android:right= "3dp" android:bottom= "3dp" />
- </shape>
--------rounded_corners_view.xml此为对话框内容的背景布局文件
- <?xml version= "1.0" encoding= "utf-8" ?>
- <shape xmlns:android= "http://schemas.android.com/apk/res/android" >
- <solid android:color= "#ff606060" />
- <stroke android:width= "3dp" color= "#ffff8080" />
- <corners android:radius= "10dp" />
- <padding android:left= "5dp" android:top= "5dp"
- android:right= "5dp" android:bottom= "5dp" />
- </shape>
2,圆角文字编辑框的三个布局XML文件
---------rounded_edittext_states.xml
- <?xml version= "1.0" encoding= "utf-8" ?>
- <selector xmlns:android= "http://schemas.android.com/apk/res/android" >
- <item
- android:state_pressed= "true"
- android:state_enabled= "true"
- android:drawable= "@drawable/rounded_focused" />
- <item
- android:state_focused= "true"
- android:state_enabled= "true"
- android:drawable= "@drawable/rounded_focused" />
- <item
- android:state_enabled= "true"
- android:drawable= "@drawable/rounded_edittext" />
- </selector>
----------rounded_edittext.xml
- <?xml version= "1.0" encoding= "utf-8" ?>
- <shape xmlns:android= "http://schemas.android.com/apk/res/android"
- android:shape= "rectangle"
- android:padding= "8dip" >
- <solid android:color= "#FFFFFF" />
- <corners
- android:bottomRightRadius= "10dip"
- android:bottomLeftRadius= "10dip"
- android:topLeftRadius= "10dip"
- android:topRightRadius= "10dip" />
- </shape>
-----------rounded_edittext_focused.xml
- <?xml version= "1.0" encoding= "utf-8" ?>
- <shape xmlns:android= "http://schemas.android.com/apk/res/android"
- android:shape= "rectangle"
- android:padding= "8dip" >
- <solid android:color= "#FFFFFF" />
- <stroke android:width= "2dip" android:color= "#FF0000" />
- <corners
- android:bottomRightRadius= "10dip"
- android:bottomLeftRadius= "10dip"
- android:topLeftRadius= "10dip"
- android:topRightRadius= "10dip" />
- </shape>
popupwindow
A popup window that can be used to display an arbitrary view. The popup windows is a floating container that appears on top of the current activity.
一个弹出窗口,可以用来显示一个任意的看法。弹出窗口是一个浮动的容器,对当前活动顶部
PopupWindow.OnDismissListener Listener that is called when this popup window is dismissed.
PopupWindow.OnDismissListener××xx被调用时,这个弹出窗口被驳回。
setOnDismissListener(PopupWindow.OnDismissListener onDismissListener)在取消弹出窗口时出发的事件。
其 PopupWindow.setInputMethodMode(INPUT_METHOD_FROM_FOCUSABLE|INPUT_METHOD_NEEDED|INPUT_METHOD_NOT_NEEDED )来设置解释如下:
INPUT_METHOD_FROM_FOCUSABLE为对输入法的要求,应根据该弹出可聚焦能力。
诠释INPUT_METHOD_NEEDED为这个弹出总是需要使用一个输入法,不管它是否是可聚焦。
诠释INPUT_METHOD_NOT_NEEDED为这个弹出从未需要与一个输入法,不管它是否是可聚焦。
PopupWindow构造方法如下:
PopupWindow(Context context)
PopupWindow(Context context, AttributeSet attrs)
PopupWindow(Context context, AttributeSet attrs, int defStyle)
PopupWindow()
PopupWindow(View contentView)
PopupWindow(int width, int height)
PopupWindow(View contentView, int width, int height)
PopupWindow(View contentView, int width, int height, boolean focusable)
常用的方法:
dismiss() 关闭弹出的窗口
getContentView
getBackground
isClippingEnabled是否裁剪启用
isFocusable是否弹出窗口可以抓住焦点
boolean isShowing() 是否表明弹出窗口显示在屏幕上
boolean isTouchable() 是否弹出窗口支持触摸事件
setClippingEnabled(boolean enabled)
setContentView( contentView) 更改弹出的内容
setFocusable(boolean focusable)
setHeight(int height) 更改弹出的高度
setOutsideTouchable(boolean touchable) 控制是否弹出窗口将被告知触摸事件的窗口外面。
void setWidth(int width)
void setTouchable(boolean touchable)
void setTouchInterceptor(View.OnTouchListener l)所有设置触摸事件回调被派往该弹出窗口
setWindowLayoutMode(int widthSpec, int heightSpec) 更改宽度和高度的措施,都是通过弹出的窗口管理规范
showAsDropDown(View anchor, int xoff, int yoff)
Display the content view in a popup window anchored to the bottom-left corner of the anchor view offset by the specified x and y coordinates.
showAsDropDown(View anchor)
Display the content view in a popup window anchored to the bottom-left corner of the anchor view.
showAtLocation(View parent, int gravity, int x, int y)显示在弹出式窗口的内容以便在指定的位置
更新
update(int width, int height)
update(View anchor, int xoff, int yoff, int width, int height)
update()
update(int x, int y, int width, int height, boolean force)
update(int x, int y, int width, int height)
update(View anchor, int width, int height)