先给大家炫下效果图:
首先过程中碰到的几个问题:
1、对 EditText 进行自定义背景
2、运行时自动 EditText 自动获得焦点
3、在获得焦点时即清空 hint ,而不是输入后清空
4、清空按钮的出现时机(在得到焦点并且有输入内容时)
.........
--- 这些问题都有一一解决 ---
以下是代码:
布局 fragment_main(问题2)
<!-- androID:focusable="true" androID:focusableIntouchMode="true" 把EditText默认的行为截断了! --><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:background="#ECEDF1" androID:focusable="true" androID:focusableIntouchMode="true" tools:context="com.dragon.androID.qqlogin.MainActivity$PlaceholderFragment" > <ImageVIEw androID:ID="@+ID/imageVIEw1" androID:layout_wIDth="70dp" androID:layout_height="70dp" androID:layout_centerHorizontal="true" androID:layout_marginBottom="5dp" androID:layout_margintop="40dp" androID:src="@drawable/a" /> <EditText androID:ID="@+ID/editText1" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_below="@ID/imageVIEw1" androID:ems="10" androID:background="@drawable/bg_edittext" androID:inputType="textPersonname" androID:gravity="center" androID:textcolor="#6A6A6C" androID:hint="@string/inaccount" androID:textcolorHint="#ECEDDD"> </EditText> <EditText androID:ID="@+ID/editText2" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_below="@ID/editText1" androID:ems="10" androID:background="@drawable/bg_edittext" androID:inputType="textPassword" androID:gravity="center" androID:textcolor="#6A6A6C" androID:hint="@string/inpwd" androID:textcolorHint="#ECEDDD" > </EditText> <button androID:ID="@+ID/button1" androID:layout_wIDth="match_parent" androID:layout_height="40dp" androID:layout_below="@ID/editText2" androID:layout_marginleft="20dp" androID:layout_marginRight="20dp" androID:layout_margintop="10dp" androID:background="@drawable/bg_button" androID:text="@string/button" androID:gravity="center" androID:textcolor="#F9FAFB" /> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_alignParentBottom="true" androID:padding="10dp" > <TextVIEw androID:ID="@+ID/textVIEw2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:gravity="center" androID:text="@string/faillogin" androID:textcolor="#0EB1EF" /> <TextVIEw androID:ID="@+ID/textVIEw3" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:gravity="right" androID:text="@string/regist" androID:textcolor="#0EB1EF" /> </linearLayout> <button androID:ID="@+ID/button2" androID:layout_wIDth="16dp" androID:layout_height="16dp" androID:layout_aligntop="@ID/editText1" androID:layout_margintop="15dp" androID:layout_alignParentRight="true" androID:layout_marginRight="10dp" androID:background="@drawable/clear" androID:visibility="invisible" /> <button androID:ID="@+ID/button3" androID:layout_wIDth="16dp" androID:layout_height="16dp" androID:layout_aligntop="@ID/editText2" androID:layout_margintop="15dp" androID:layout_alignleft="@+ID/button2" androID:background="@drawable/clear" androID:visibility="invisible" /></relativeLayout>fragment_main
button 和 EditText 的背景(问题1)
<?xml version="1.0" enCoding="utf-8"?><shape xmlns:androID="http://schemas.androID.com/apk/res/androID" > <stroke androID:wIDth="1px" androID:color="#00ACED" /> <solID androID:color="#00ACED" /> <corners androID:radius="10dp" /></shape>bg_button
<?xml version="1.0" enCoding="utf-8"?><shape xmlns:androID="http://schemas.androID.com/apk/res/androID" > <stroke androID:wIDth="1px" androID:color="#ECEDF1" /> <solID androID:color="#F9FAFB" /> <corners androID:radius="10dp" /> <padding androID:top="10dp" androID:bottom="10dp"/></shape>bg_edittext
<?xml version="1.0" enCoding="utf-8"?><resources> <string name="app_name">qqloginnew</string> <string name="action_settings">Settings</string> <string name="button">登录</string> <string name="faillogin">无法登录?</string> <string name="regist">新用户注册</string> <string name="inaccount">QQ号/手机号/邮箱</string> <string name="inpwd">密码</string> <string name="sucess">登录成功</string></resources>strings
MainActivity (问题3、4.....)
package com.dragon.androID.qqloginnew;import androID.app.Activity;import androID.os.Bundle;import androID.text.Editable;import androID.text.TextWatcher;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.vIEw.VIEw.OnFocuschangelistener;import androID.Widget.button;import androID.Widget.EditText;public class MainActivity extends Activity {private EditText editText1;private EditText editText2;// private button button;private button clearbutton1;private button clearbutton2;// 得到strings中的属性// private String string2 = getResources().getString(R.string.inaccount);@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentVIEw(R.layout.fragment_main);editText1 = (EditText) findVIEwByID(R.ID.editText1);editText2 = (EditText) findVIEwByID(R.ID.editText2);// button = (button) findVIEwByID(R.ID.button1);clearbutton1 = (button) findVIEwByID(R.ID.button2);clearbutton2 = (button) findVIEwByID(R.ID.button3);// 对EditText进行焦点变更监听editText1.setonFocuschangelistener(new EditTextListener(clearbutton1));editText2.setonFocuschangelistener(new EditTextListener(clearbutton2));// 对清空按钮进行点击监听clearbutton1.setonClickListener(new ClearbuttonListener());clearbutton2.setonClickListener(new ClearbuttonListener());// 对EditText进行编辑监听editText1.addTextChangedListener(new MyEditTextWatcher(editText1));editText2.addTextChangedListener(new MyEditTextWatcher(editText2));}/*** 对EditText的内容进行实时监控* * @author Auser* */class MyEditTextWatcher implements TextWatcher {private CharSequence temp;private EditText editText;public MyEditTextWatcher(EditText editText) {this.editText = editText;}@OverrIDe// int start开始的位置,int count被改变的旧内容数,int after改变后的内容数量public voID beforeTextChanged(CharSequence s,int start,int count,int after) {// 这里的s表示改变之前的内容,通常start和count组合,可以在s中读取本次改变字段中被改变的内容。而after表示改变后新的内容的数量。}@OverrIDe// int start开始的位置,int before改变前的内容数量,int count新增量public voID onTextChanged(CharSequence s,int before,int count) {// 这里的s表示改变之后的内容,通常start和count组合,可以在s中读取本次改变字段中新的内容。而before表示被改变的内容的数量。temp = s;}@OverrIDe// 表示最终内容public voID afterTextChanged(Editable s) {if (temp.length() > 0) {// 设置清空按钮为可见if (editText == editText1) {clearbutton1.setVisibility(VIEw.VISIBLE);} else if (editText == editText2) {clearbutton2.setVisibility(VIEw.VISIBLE);}} else {// 设置清空按钮不可见if (editText == editText1) {clearbutton1.setVisibility(VIEw.INVISIBLE);} else if (editText == editText2) {clearbutton2.setVisibility(VIEw.INVISIBLE);}}}}/*** 清空按钮点击事件* * @author* */class ClearbuttonListener implements OnClickListener {@OverrIDepublic voID onClick(VIEw vIEw) {if (vIEw == clearbutton1) {editText1.setText("");} else if (vIEw == clearbutton2) {editText2.setText("");}}}/*** 焦点变更事件* * @author Auser* */class EditTextListener implements OnFocuschangelistener {private button clear;public EditTextListener(button clear) {this.clear = clear;}@OverrIDepublic voID onFocusChange(VIEw v,boolean hasFocus) {EditText textVIEw = (EditText) v;String hint;if (hasFocus) {// 当获取焦点时如果内容不为空则清空按钮可见if (!textVIEw.getText().toString().equals("")) {clear.setVisibility(VIEw.VISIBLE);}// if (textVIEw == editText2) {// // 设置输入格式为不可见的密码格式// textVIEw.setinputType(inputType.TYPE_CLASS_TEXT// | inputType.TYPE_TEXT_VARIATION_PASSWORD);// }hint = textVIEw.getHint().toString();// 给TextVIEw添加额外的数据textVIEw.setTag(hint);textVIEw.setHint("");} else {// 当失去焦点时清空按钮不可见clear.setVisibility(VIEw.INVISIBLE);// if (textVIEw == editText2) {// // 设置输入格式为可见的密码格式// textVIEw.setinputType(inputType.TYPE_CLASS_TEXT// | inputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);// }// 取出之前添加的额外数据hint = textVIEw.getTag().toString();textVIEw.setHint(hint);}}}}
下篇文章给大家介绍Android实现QQ新用户注册界面遇到问题及解决方法,感兴趣的朋友可以参考下。
以上所述是小编给大家介绍的AndroID实现QQ登录界面遇到问题及解决方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android实现QQ登录界面遇到问题及解决方法全部内容,希望文章能够帮你解决Android实现QQ登录界面遇到问题及解决方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)