一 界面设计
1 代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@color/colorPrimary"
android:gravity="center_horizontal"
android:text="LoginDemo"
android:textColor="#FFFFFF"
android:textSize="30sp" />
<LinearLayout
android:id="@+id/layout_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_logo"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical">
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入账号" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:inputType="textPassword" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout_login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/layout_input"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:text="登录" />
<Button
android:id="@+id/btn_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="注册" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/layout_login_button"
android:layout_centerHorizontal="true"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>
2 设计视图
二 Java代码
package com.example.logindemo;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//定义两个TextView
private TextView et_username;
private TextView et_password;
//定义两个Button
private Button btn_login;
private Button btn_register;
//声明事件一个事件监听器并实现View.OnClickListener接口
private View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
//取得view的ID号
int id = view.getId();
//通过switch判断是哪一个控件被点击了,并执行相应的动作
switch (id) {
case R.id.btn_login:
String username = et_username.getText().toString();
String password = et_password.getText().toString();
Log.d("Username:", username);
Log.d("Password:", password);
progressDialog.show();
break;
case R.id.btn_register:
break;
}
}
};
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//将UI控件进行绑定
et_username = (TextView) findViewById(R.id.et_username);
et_password = (TextView) findViewById(R.id.et_password);
btn_login = (Button) findViewById(R.id.btn_login);
btn_register = (Button) findViewById(R.id.btn_register);
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("正在登录,请稍等...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//给按钮设置点击事件监听器
btn_login.setOnClickListener(onClickListener);
btn_register.setOnClickListener(onClickListener);
}
}
三 日志打印
08-04 08:57:00.521 2671-2671/com.example.logindemo D/Username:: cakin
08-04 08:57:00.521 2671-2671/com.example.logindemo D/Password:: cakin
四 运行效果