效果
主页面
package com.example.zy_denglu; import android.app.ProgressDialog; import android.os.SystemClock; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText mUsername; private EditText mPassword; private Button login; private ProgressDialog mProgressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { mUsername = (EditText) findViewById(R.id.username); mPassword = (EditText) findViewById(R.id.password); login = (Button) findViewById(R.id.login); login.setOnClickListener(this); //动态的创建一个进度条 mProgressDialog = new ProgressDialog(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.login: //获取用户输入的信息 final String username = mUsername.getText().toString().trim(); final String password = mPassword.getText().toString().trim(); //创建Bean类,把数据放到Bean类里 final User user = new User(); user.username = username; user.password = password; //判断信息是否为null boolean userInfo = submit(user); if (userInfo) { //显示进度条对话框 mProgressDialog.show(); //开个子线程做耗时操作 new Thread() { public void run() { //模拟请求网络,睡一会在进行操作 SystemClock.sleep(2000); //关闭进度条 mProgressDialog.dismiss(); //对用户输入的信息进行判断 if ("123123".equals(user.username) && "123".equals(user.password)) { //更新UI runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "欢迎回来,思密达!!", Toast.LENGTH_SHORT).show(); } }); } else { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "您的信息有误,请重新填写!!", Toast.LENGTH_SHORT).show(); } }); } } ; }.start(); } else { Toast.makeText(this, "输入不能为null,么么哒!!!", Toast.LENGTH_SHORT).show(); } break; } } private boolean submit(User user) { if (TextUtils.isEmpty(user.username) || TextUtils.isEmpty(user.password)) { Toast.makeText(this, "输入不能为空", Toast.LENGTH_SHORT).show(); return false; } return true; } }
定义的User类
package com.example.zy_denglu; /** * data:2018/05/17. * author : 殷成龙(Administrator) * function : */ class User { public String username; public String password; }
布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.zy_denglu.MainActivity"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名"/> <EditText android:layout_width="300dp" android:layout_height="wrap_content" android:id="@+id/username"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码"/> <EditText android:layout_width="300dp" android:layout_height="wrap_content" android:id="@+id/password"/> </LinearLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登陆" android:id="@+id/login"/> </LinearLayout>