- 浏览: 125489 次
- 性别:
- 来自: 深圳
最新评论
-
T_bag:
...
TabHost 中的Activity执行顺序 -
ihopethatwell:
楼主,你能否写一个 int类型的一维数组的结构体?
linux NDK实例 -
gf_crazy:
刚好找第二种,其他地方全是第一种。
TabHost -
gangbener:
我们是可以把不同分辨率的图片放到不同的图片文件夹中去,问题是: ...
android程序中屏幕问题解决方案 -
shusanzhan:
学习了,Mark
android应用收费渠道
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);
- }
- }
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>
<?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>
<?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>
<?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>
<?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>
<?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>
3,对话框的布局文件popupwindow.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent" android:layout_height="wrap_content"
- android:orientation="vertical">
- <TextView android:id="@+id/username_view"
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:layout_marginLeft="10dip"
- android:layout_marginRight="10dip"
- android:text="用户名"
- android:textAppearance="?android:attr/textAppearanceMedium"/>
- <EditText android:id="@+id/username_edit"
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:layout_marginLeft="10dip"
- android:layout_marginRight="10dip"
- android:capitalize="none"
- android:textAppearance="?android:attr/textAppearanceMedium" />
- <TextView android:id="@+id/password_view"
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:layout_marginLeft="10dip"
- android:layout_marginRight="10dip"
- android:text="密码"
- android:textAppearance="?android:attr/textAppearanceMedium"/>
- <EditText android:id="@+id/password_edit"
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:layout_marginLeft="10dip"
- android:layout_marginRight="10dip"
- android:capitalize="none"
- android:password="true"
- android:textAppearance="?android:attr/textAppearanceMedium" />
- <LinearLayout android:id="@+id/LinearLayout01"
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:gravity="center"
- android:paddingLeft="10dip"
- android:paddingRight="10dip">
- <Button android:id="@+id/BtnOK"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="确定"/>
- <Button android:id="@+id/BtnCancel"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="取消"/>
- </LinearLayout>
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/username_view" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:text="用户名" android:textAppearance="?android:attr/textAppearanceMedium"/> <EditText android:id="@+id/username_edit" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:capitalize="none" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/password_view" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:text="密码" android:textAppearance="?android:attr/textAppearanceMedium"/> <EditText android:id="@+id/password_edit" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:capitalize="none" android:password="true" android:textAppearance="?android:attr/textAppearanceMedium" /> <LinearLayout android:id="@+id/LinearLayout01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="center" android:paddingLeft="10dip" android:paddingRight="10dip"> <Button android:id="@+id/BtnOK" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="确定"/> <Button android:id="@+id/BtnCancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="取消"/> </LinearLayout> </LinearLayout>
4,主布局文件 main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:paddingTop="10dip">
- <EditText android:id="@+id/roundedtext1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="圆角编辑框实例"
- android:padding="5dip"
- android:background="@drawable/rounded_edittext" />
- <!-- 此View为布局使用 -->
- <View android:layout_height="5dip" android:layout_width="fill_parent"/>
- <EditText android:id="@+id/roundedtext2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="聚焦可变边框颜色"
- android:padding="5dip"
- android:paddingTop="30dip"
- android:background="@drawable/rounded_edittext_states"/>
- <!-- 此View为布局使用 -->
- <View android:layout_height="5dip" android:layout_width="fill_parent"/>
- <Button android:id="@+id/Button01"
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:text="弹出圆角对话框"/>
- </LinearLayout>
发表评论
-
android.os.NetworkOnMainThreadException
2011-12-24 13:14 1244不能在android的主线程中,执行一个网络操作 ... -
转载:为什么要对URI进行编码
2011-12-15 15:49 1091为什么需要Url编码,通常如果一样东西需要编码,说明这样东 ... -
multipart form-data boundary
2011-12-15 15:23 1033含义 ENCTYPE="multipart/ ... -
android http 附件
2011-12-15 14:17 1673一:服务器端: 1:struts-config.xml ... -
post 附件
2011-12-15 10:24 1017在做嘀咕客户端的时候,要实现拍照上传的功能。根据嘀咕api ... -
让Android应用获取系统权限
2011-12-08 18:46 1013在 android 的API中有提供 SystemCloc ... -
Android源码目录结构详解
2011-12-01 20:22 864Android 2.1 |-- Makefile |-- ... -
两个activity跳转
2011-11-25 16:06 1289Activity A跳转到 Activity B /**A. ... -
线程同步之wait()/notify()的使用
2011-11-21 11:24 1019wait()/notify() 通常,多 ... -
游戏中渲染线程与更新线程交替执行
2011-11-21 11:21 959private final State mThreadLock ... -
android colormatrix
2011-11-03 17:32 1509在编程中有时候需要 ... -
java栈,堆,池
2011-07-08 09:38 760今天复习了一下这些知识,顺便做了下笔记.1.寄存器:最快的存储 ... -
3D开发的境界
2011-06-04 20:12 714第一阶段:初学者阶 ... -
Http
2011-06-01 17:10 1323使用 HTTP 服务: 1. Apache HttpCline ... -
获取手机的Opengl的支持版本
2011-05-27 09:28 1472public int getGLVersion() { ... -
性能优化
2011-05-27 09:26 801如果你想写一个 Java 程序,观察某对象什么时候会被垃圾收集 ... -
Android游戏中其他语言数据类型之间的转换方法
2011-05-17 11:43 1244Java与其他语言数据类型之间的转换方法实例程序 /* ... -
android canvas.getClipBounds
2011-05-13 17:41 8444一种是传参数: Rect dstRect = new Re ... -
获取屏幕大小的方法
2011-05-13 17:38 613// one DisplayMetrics dm = n ... -
Android Lock 使用
2011-05-13 16:43 3226PowerManager 和PowerManager.Wa ...
相关推荐
与Dialog不同,PopWindow更加灵活,它可以显示在屏幕的任何位置,且不需要遵循Activity的生命周期。在`CustomDialogDemo`中,开发者可能创建了一个自定义的PopupWindow类,包含了自定义的布局和触摸事件处理。...
在`PopwindowAndDialog`文件中,可能包含了这些示例的代码实现,包括`PopWindow`和`Dialog`的布局文件、Java或Kotlin类以及显示它们的方法。通过阅读和理解这些代码,开发者可以更好地掌握如何在实际项目中运用`...
与Dialog不同,PopWindow没有预设的外观,完全由开发者自定义。它常用于快速操作或者显示额外的信息,如下拉菜单、快捷工具栏等。创建PopWindow通常涉及创建一个布局,然后通过PopupWindow类实例化并显示。 5. **...
不同于Dialog,PopWindow并不继承自Dialog类,而是直接继承自ViewGroup,因此在使用上具有更大的灵活性。在创建PopWindow时,我们需要定义其内容视图、宽度和高度。 实现"基于PopWindow的底部菜单栏",首先需要创建...
1. **创建Dialog类**: 创建一个继承自`AppCompatDialog`的类,并重写`onCreate()`方法,加载自定义布局。 ```java public class CustomDialog extends AppCompatDialog { public CustomDialog(Context context, int...
与Dialog相比,PopWindow更加灵活,可以在界面上浮动,而Dialog则总是位于Activity之上。 在描述中提到的“focus问题”,可能是指当PopWindow弹出时,与之相关的焦点管理问题。例如,如果PopWindow中包含可点击的...
相比于Dialog,`PopWindow`不会阻塞用户与背景视图的交互,更加灵活。 实现多级菜单的核心思路是通过递归或者循环的方式,为每一级菜单创建并显示`PopWindow`。以下是一个简单的步骤概述: 1. **创建PopWindow**:...
在“CustomDialogDemo”源码中,你将看到如何将这些概念应用于实际项目中,包括如何组织XML布局文件、如何在Java代码中实例化和控制对话框与PopWindow,以及如何响应用户交互。通过学习和研究这个示例,开发者可以更...
3. 考虑不同设备尺寸:在设计PopWindow布局时,需考虑不同屏幕尺寸,确保在各种设备上显示正常。 总之,PopWindow是Android开发中的一个重要工具,理解和熟练使用PopWindow能够极大地提升应用的交互体验。通过...
PopWindow是Android中的一个轻量级窗口,它不像Dialog那样需要完整的主题背景,而是可以像一个小部件一样浮现在屏幕的任意位置。通常用于快速操作或展示临时信息。 实现自定义PopWindow的第一步是创建布局文件。在...
与Dialog不同,PopWindow并不阻塞用户与背景界面的交互,因此更适合创建类似快捷菜单的效果。 在`描述`中提到,通过点击菜单项可以触发相应的动画效果,这涉及到对PopWindow的自定义以及动画的设置。实现这一功能...
这个项目不采用`PopupWindow`和`Dialog`来构建筛选界面,而是选择了其他方式来达成这一目标,可能是为了提供更自定义的用户体验或者解决特定场景下的限制。下面将详细解释这种非`PopupWindow`和`Dialog`的筛选实现...
与Dialog相比,它不需要主题风格,可以在任意位置显示,并且可以自定义布局。 - 创建PopWindow的基本步骤包括:创建一个View,通过PopupWindow类实例化,设置宽度、高度、背景透明度等属性,最后调用showAsDropDown...
1. 创建布局资源文件,定义PopWindow的显示内容。 2. 使用`LayoutInflater`加载布局。 3. 创建`PopupWindow`对象,传入加载好的布局视图和宽度、高度。 4. 设置是否允许点击PopWindow外部关闭、背景透明度等属性。 5...
在Android应用设计中,PopWindow提供了比对话框(Dialog)更多的灵活性,因为它可以自定义大小、位置,并且不会阻塞用户与背景界面的交互。本篇文章将深入探讨PopWindow的原理、使用方法以及如何实现自定义功能。 ...
在Android开发中,自定义对话框(Dialog)和PopWindow是两种常见的用户界面组件,用于增强用户体验和提供交互式操作。这些组件可以帮助开发者在主应用界面之上显示临时性信息或功能,比如设置、选择列表、通知等。在...
`PopWindow`是Android中的一个轻量级窗口,它不像`Dialog`那样需要依附于Activity,而是可以直接显示在屏幕的任意位置。它可以包含任意的View,如按钮、文本、列表等,因此非常适合用来构建各种交互界面。 自定义`...
PopWindow通常用于实现下拉菜单、提示信息等效果,与Dialog相比,它的使用更加灵活,不会阻塞整个应用程序的交互。 实现"更多按钮"的PopWindow,需要以下步骤: 1. 创建PopWindow的布局:在XML中定义PopWindow显示...