下面把代码贴上来吧,大虾就勿喷了!
LoginActivity.java
[java]
package com.challen;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
/**
* Activity which displays a login screen to the user, offering registration as
* well.
*/
public class LoginActivity extends Activity {
private EditText mPasswordView;
private TextView mLoginStatusMessageView;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLoginStatusMessageView = (TextView)findViewById(R.id.sign_in_button);
mPasswordView = (EditText) findViewById(R.id.password);
mPasswordView.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(mPasswordView.getText().toString().equals("123456")){
intent = new Intent(LoginActivity.this,secondActivity.class);
startActivity(intent);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
@Override
protected void onResume() {
// 由于我知道你会点击返回键反复试验,所以加了这句话
mPasswordView.setText("");
super.onResume();
}
}
跳转过去的second.activity各位就随便弄一个吧!不过需要记住的就是新增了activity后记着要在AndroidManifest.xml中添加这个activity
mian.xml
[
html]
xmlns:tools="http://schemas.android.com/tools"
tools:context=".LoginActivity" >
style="@style/LoginFormContainer"
android:orientation="vertical" >
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入密码"
android:maxLines="1"
android:singleLine="true" />
android:id="@+id/sign_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="16dp"
android:paddingLeft="32dp"
android:paddingRight="32dp"
android:text="密码匹配成功则自动登录" />
其实最关键的就是知道edittext的一些监听的函数了,例如还有一些自动补全特别是用于账号输入的时候,这里主要就是用到了监听edittext的变化用到了addTextChangedListener,并且配合TextWatcher()就能实现在输入的过程中进行一些操作了,把登录跳转的方法写在了onTextChanged里。
好了,我这里预设的密码就是123456设置死了的,大家可以试一试啦!