本文主要是介绍Android 登录界面的实现,详细请看代码:
1.MainActivity.java
public class MainActivity extends Activity implements OnClickListener{
private EditText mUsername;
private EditText mPassword;
private ImageView mClearUsername;
private ImageView mClearPassword;
private Button mLoginButton;
private Button mRegister;
private String username;
private String password;
private String TAG="LoginActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 隐藏标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initUI();
mLoginButton.setOnClickListener(this);
mRegister.setOnClickListener(this);
mUsername.setOnClickListener(this);
mClearUsername.setOnClickListener(this);
mClearPassword.setOnClickListener(this);
mUsername.setOnFocusChangeListener(mFocusChangeListener);
mPassword.setOnFocusChangeListener(mFocusChangeListener);
}
/**
* 初始化UI界面
*/
public void initUI(){
mUsername = (EditText) findViewById(R.id.username);
mPassword = (EditText) findViewById(R.id.user_password);
mLoginButton = (Button) findViewById(R.id.log_btn);
mRegister = (Button) findViewById(R.id.register_btn);
TextPaint tp= mLoginButton.getPaint();
tp.setFakeBoldText(true);
mClearUsername = (ImageView) findViewById(R.id.iv_clear_username);
mClearPassword = (ImageView) findViewById(R.id.iv_clear_password);
}
@Override
public void onClick(View v) {
username = mUsername.getText().toString().trim();//这里trim()作用是去掉首位空格,防止不必要的错误
Log.d(TAG, "用户填写的用户名:"+ username);
password = mPassword.getText().toString().trim();
Log.d(TAG, "用户填写的密码:"+password);
switch (v.getId()) {
case R.id.iv_clear_username:
mUsername.setText("");
break;
case R.id.iv_clear_password:
mPassword.setText("");
break;
case R.id.register_btn:
//写跳转注册界面的逻辑
finish();
break;
case R.id.log_btn:
/**
* 监听网络状态
*/
if(isNetworkAvailable())
{
//此处可以从服务器下载用户信息并验证信息是否相符,然后跳转界面
}else {
Toast.makeText(getApplicationContext(), "请确认网络连接!",
Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
View.OnFocusChangeListener mFocusChangeListener = new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
int id = view.getId();
switch (id) {
case R.id.username:
mClearUsername.setVisibility(hasFocus ? View.VISIBLE : View.GONE);
break;
case R.id.user_password:
mClearPassword.setVisibility(hasFocus ? View.VISIBLE : View.GONE);
break;
default:
break;
}
}
};
/**
* 网络连接
* @return
*/
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isAvailable();
}
/**
* 返回键监听
*/
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK){
finish();
}
return false;
}
@Override
protected void onDestroy() {
super.onDestroy();
System.exit(0);
Log.d(TAG, "退出系统");
finish();
}
}
2.布局文件:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="@drawable/login_background"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="46dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="22dp"
android:orientation="horizontal">
<TextView
android:text="用户名"
android:layout_width="70dip"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="20sp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/input_bg"
android:gravity="end"
android:orientation="horizontal" >
<EditText
android:id="@+id/username"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@null"
android:hint="请输入用户名"
android:inputType="phone"
android:textColor="#ff353535"
android:textSize="15sp" />
<ImageView
android:id="@+id/iv_clear_username"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:scaleType="centerInside"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:visibility="gone"
android:background="@drawable/search_clear_pressed"
android:contentDescription="@null"
tools:visibility="visible" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="46dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="22dp"
android:orientation="horizontal">
<TextView
android:layout_width="70dip"
android:layout_height="match_parent"
android:gravity="center"
android:text="密码"
android:textColor="#ffffff"
android:textSize="20sp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/input_bg">
<EditText
android:id="@+id/user_password"
android:maxLength="8"
android:singleLine="true"
android:digits="._0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLIMNOPQRSTUVWXYZ"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@null"
android:hint="请输入密码"
android:inputType="textPassword"
android:textColor="#ff353535"
android:textSize="15sp"/>
<ImageView
android:id="@+id/iv_clear_password"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:scaleType="centerInside"
android:visibility="gone"
android:background="@drawable/search_clear_pressed"
android:contentDescription="@null"
tools:visibility="visible"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:layout_marginTop="22dp"
android:gravity="center_horizontal">
<Button
android:id="@+id/log_btn"
android:layout_width="80dip"
android:layout_height="40dip"
android:text="登录"
>
</Button>
<Button
android:id="@+id/register_btn"
android:layout_width="80dip"
android:layout_height="40dip"
android:layout_marginLeft="10dip"
android:text="注册"
>
</Button>
</LinearLayout>
</LinearLayout>
</LinearLayout>
3.配置文件:AndroidManifest.xml
记得添加相应权限