鱼鱼Chen之学写自己的apk(四)灵活使用EditText的hint属性以及使用自定义的美观AlertDialog
2015-04-22 20:30
519 查看
EditText应该也算是常用的组件了。不过我们这篇仅对hint属性做一些有意思的功能。
一般来说,未编辑的EditText有个提示,而编辑过后就消失。当然,你什么都没编辑的话,提示又会出现~
效果如下:
![](http://img.blog.csdn.net/20150422225827094?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemVyb2xvdmVzYzE5OTM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20150422225847374?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemVyb2xvdmVzYzE5OTM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20150422230021449?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemVyb2xvdmVzYzE5OTM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20150422225917404?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemVyb2xvdmVzYzE5OTM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
不会用手机做动图,将就一下。
一、关于EditText的hint实现方法
主布局如下 activity_main.xml
android:focusable="true"
android:focusableInTouchMode="true"
这两句让当前布局能获得焦点。
然后是主Activitiy
初始化
二、关于自定义的AlertDialog
![](http://img.blog.csdn.net/20150422232518178?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemVyb2xvdmVzYzE5OTM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
小火鸡,萌萌哒~~
还是先从布局说起,新建一个dialog_layout.xml
到主Activity,重写onBackPressed方法。去掉super(因为这句话调用了父类的按下返回键会关掉Activity)。
然后,我们这么写。
下面是个正常的调用系统的布局的写法,方便对比。(此时使用的是AlertDialog.Builder类的对象,并非是AlertDialog的对象,要注意!)
并不复杂的两个知识点,但是用起来很舒服,还能使你的应用增色不少~
完整的代码(百度云链接):http://pan.baidu.com/s/1dDjKcLj
一般来说,未编辑的EditText有个提示,而编辑过后就消失。当然,你什么都没编辑的话,提示又会出现~
效果如下:
不会用手机做动图,将就一下。
一、关于EditText的hint实现方法
主布局如下 activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:focusable="true" android:focusableInTouchMode="true" android:orientation="vertical" android:padding="8dp" tools:context="com.dota.example.fishychenofeditanddialog.MainActivity" > <EditText android:id="@+id/editor01" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名" /> <EditText android:id="@+id/editor02" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入数字密码" android:layout_below="@+id/editor01" android:inputType="numberPassword" android:maxLength="6" /> <TextView android:id="@+id/textview_01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这只是个做测试的文本" android:textSize="25sp" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true"/> </RelativeLayout>这边都是很基本的,就不解释了。记得!在最外部布局加上
android:focusable="true"
android:focusableInTouchMode="true"
这两句让当前布局能获得焦点。
然后是主Activitiy
初始化
private void init() { editor01 = (EditText) findViewById(R.id.editor01); editor02 = (EditText) findViewById(R.id.editor02); editor01.setOnFocusChangeListener(this); editor02.setOnFocusChangeListener(this); }关键就在这儿
@Override public void onFocusChange(View v, boolean hasFocus) { ((EditText) v).setHint(""); switch (v.getId()) { case R.id.editor01: if (editor01.getText().toString().equals("") && hasFocus == false) { editor01.setHint(R.string.text_user); } break; case R.id.editor02: if (editor02.getText().toString().equals("") && hasFocus == false) { editor02.setHint(R.string.text_passweord); } break; } }这边简单说一下,点击对应EditText的时候会获得焦点,如果当时有一个有焦点,就会失去。那么都会触发焦点变化,此时hasFocus传入的是是否有焦点。当焦点变化时,设置提示为“”,并检测,如果此时没有写内容并且失去焦点,那么再显示提示。
二、关于自定义的AlertDialog
小火鸡,萌萌哒~~
还是先从布局说起,新建一个dialog_layout.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/imageView_background" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="120dp" android:adjustViewBounds="true" android:background="@drawable/pokenmen" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="40dp" android:background="@drawable/no255" /> <TextView android:id="@+id/text_out" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/imageView_background" android:layout_alignParentLeft="true" android:layout_marginBottom="100dp" android:layout_marginLeft="80dp" android:background="@drawable/background_text" android:text="退出" android:textColor="#ffbc53" android:textSize="30sp" android:textStyle="bold" /> <TextView android:id="@+id/text_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/imageView_background" android:layout_alignParentRight="true" android:layout_marginBottom="100dp" android:layout_marginRight="80dp" android:background="@drawable/background_text" android:text="取消" android:textColor="#ffbc53" android:textSize="30sp" android:textStyle="bold" /> </RelativeLayout>原谅我不会用Layer-list做出这样的效果(至少不是我要的)。这边记得将父布局的高设为wrap_content,还有就是文本的背景用shape.xml来写。别的没什么要注意的了。
到主Activity,重写onBackPressed方法。去掉super(因为这句话调用了父类的按下返回键会关掉Activity)。
然后,我们这么写。
@Override public void onBackPressed() { final AlertDialog dialog=new AlertDialog.Builder(this).create(); dialog.show(); dialog.setContentView(R.layout.dialog_layout); dialog.setCancelable(false); TextView btnOut=(TextView) dialog.findViewById(R.id.text_out); TextView btnCancel=(TextView) dialog.findViewById(R.id.text_cancel); btnOut.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); btnCancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog.cancel(); } }); }其实,写下来很简单的。先用AlertDialog.Builder类的create方法创建实例。再用setcontentView设置布局,setCancelable设置是否可以通过按返回关闭。dialog.cancel用来关闭对话框。这边一定要注意!!创建好了dialog之后,要先调用show方法,才能重新设置自定义的布局。不然会报错!
下面是个正常的调用系统的布局的写法,方便对比。(此时使用的是AlertDialog.Builder类的对象,并非是AlertDialog的对象,要注意!)
AlertDialog.Builder dialog2=new AlertDialog.Builder(this); dialog2.setCancelable(false); dialog2.setTitle("标题"); dialog2.setMessage("这是内容信息"); dialog2.setPositiveButton("确认按钮", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }); dialog2.setNegativeButton("取消按钮", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } });都用text表示出每句的意思了,就不一一解释了。
并不复杂的两个知识点,但是用起来很舒服,还能使你的应用增色不少~
完整的代码(百度云链接):http://pan.baidu.com/s/1dDjKcLj
相关文章推荐
- c# 特性/属性(Attribute) 以及使用反射查看自定义特性
- 自定义View如何定义和使用自己的属性
- asp.net中自定义控件ascx增加自定义属性以及给该属性使用绑定数据的解决方案
- Android 创建自己的证书文件,使用signapk.jar为apk以及zip文件签名
- 自定义AlertDialog的实现以及添加属性设置背景
- c# 特性/属性(Attribute) 以及使用反射查看自定义特性
- 鱼鱼Chen之学写自己的apk(五)使用Handler完成Android里的多线程操作实例
- android 自定义圆角头像以及使用declare-styleable进行配置属性解析
- Android 对Layout_weight属性完全解析以及使用ListView来实现表格(自定义适配器)
- c# 特性/属性(Attribute) 以及使用反射查看自定义特性
- 自定义View以及View的属性的使用(重点,xml中定义属性的值),包括Bitmap存为jpg样式(可以模仿修改照片不喜欢的地方)
- 带你一起瞧瞧自定义属性以及自定义View的使用
- C#如何创建自定义控件以及添加自定义属性和事件使用
- HTML5新特性: 自定义属性前缀data-以及dataset的使用
- 使用属性动画以及自定义view实现图片的显示与隐藏
- TextInputLayout使用以及EditText自己实现监听
- 使用属性动画以及自定义view实现图片的显示与隐藏
- c# 特性/属性(Attribute) 以及使用反射查看自定义特性
- AlertDialog实现自定义布局以及解决EditText无法弹出软键盘或者软键盘弹出错误问题
- Android中AlertDialog以及自定义对话框的使用