当前位置: 移动技术网 > IT编程>移动开发>Android > Android实现QQ登录界面遇到问题及解决方法

Android实现QQ登录界面遇到问题及解决方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

柬,笹仓杏,呆呆精灵国语

先给大家炫下效果图:

首先过程中碰到的几个问题:

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);
@override
protected 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 start, 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 {
@override
public 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;
}
@override
public 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登录界面遇到问题及解决方法,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网