设计目标
- 密码账户的输入
- 输入账户时,自动显示匹配账户
- 没有帐号,显示官网超链接
登录框的自动提示功能参考一下链接中的AutoCompleteTextView的使用
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_login">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="15dip"
android:layout_margin="15dip"
android:background="@drawable/background_login_div">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:text="@string/login_label_username"
android:id="@+id/login_user_input"
/>
<EditText
android:id="@+id/username_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/login_username_hint"
android:layout_below="@+id/login_user_input"
android:singleLine="true"
android:inputType="text"
/>
<TextView
android:id="@+id/login_passwd_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/username_edit"
android:layout_marginTop="3dp"
android:text="@string/login_label_passwd"/>
<EditText
android:id="@+id/passwd_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/login_passwd_input"
android:password="true"
android:singleLine="true"
android:inputType="textPassword"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/login_button"
android:layout_below="@id/passwd_edit"
android:layout_alignRight="@id/passwd_edit"
android:text="@string/login_label_signin"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/infoWords"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login_register_link"
android:layout_marginLeft="15dp"
android:textColor="#888"
android:textColorLink="#FF0066CC"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/cat"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="25dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="25dp"
android:id="@+id/logo"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo"
android:layout_toLeftOf="@id/logo"
android:layout_alignBottom="@id/logo"
android:layout_marginBottom="25dp"
/>
</RelativeLayout>
</LinearLayout>
编程代码
public class LoginActivity extends Activity {
private static final String TAG = "LoginActivity" ;
Button loginButton;
EditText username_edit;
EditText passwd_edit;
TextView infoWords;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.login);
username_edit = (EditText)findViewById(R.id.username_edit);
passwd_edit = (EditText)findViewById(R.id.passwd_edit);
infoWords = (TextView)findViewById(R.id.infoWords);
infoWords.setMovementMethod(LinkMovementMethod.getInstance());
loginButton =(Button) findViewById(R.id.login_button);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String user=username_edit.getText().toString().trim();
String passwd=passwd_edit.getText().toString().trim();
if(checkAcount(user,passwd)){
setResult(RESULT_OK);
finish();
}
else {
Toast.makeText(LoginActivity.this, "用户名密码不正确,请重新输入!", Toast.LENGTH_LONG).show();
}
}
});
}
boolean checkAcount(String user,String passwd){
// TODO: 2015/11/9
Log.w(TAG,"checkAcconut "+user + passwd);
if(user == "test" && passwd == user){
return true;
}
return false;
}
}
在TextView中设置超链接
有两种方式
第一种
<string name="login_register_link">没有帐号,<a href='//www.greatytc.com/users/2097e21ffc33/latest_articles'>注册</a></string>
<TextView
android:id="@+id/infoWords"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login_register_link"
android:layout_marginLeft="15dp"
android:textColor="#888"
android:textColorLink="#FF0066CC"/>
textView.setMovementMethod(LinkMovementMethod.getInstance());
第二种
String html = "有问题:\n";
html+="www.baidu.com";//这里即使不加协议好HTTP;也能自动被系统识别出来。
textView.setText(html);
textView.setAutoLinkMask(Linkify.ALL);
textView.setMovementMethod(LinkMovementMethod.getInstance());