今天继续完善登录、注册功能,看似简单,实则不易
Mainactivity
package com.example.demo2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private DBOpenHelper mDBOpenHelper;
private TextView mTvLoginactivityRegister;
private RelativeLayout mRlLoginactivityTop;
private EditText mEtLoginactivityUsername;
private EditText mEtLoginactivityPassword;
private LinearLayout mLlLoginactivityTwo;
private Button mBtLoginactivityLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
mDBOpenHelper = new DBOpenHelper(this);
}
private void initView() {
// 初始化控件
mBtLoginactivityLogin = findViewById(R.id.bt_loginactivity_login);
mTvLoginactivityRegister = findViewById(R.id.tv_loginactivity_register);
mRlLoginactivityTop = findViewById(R.id.rl_loginactivity_top);
mEtLoginactivityUsername = findViewById(R.id.et_loginactivity_username);
mEtLoginactivityPassword = findViewById(R.id.et_loginactivity_password);
mLlLoginactivityTwo = findViewById(R.id.ll_loginactivity_two);
// 设置点击事件监听器
mBtLoginactivityLogin.setOnClickListener(this);
mTvLoginactivityRegister.setOnClickListener(this);
}
public void onClick(View view) {
switch (view.getId()) {
// 跳转到注册界面
case R.id.tv_loginactivity_register:
startActivity(new Intent(this, item1Activity.class));
finish();
break;
case R.id.bt_loginactivity_login:
String name = mEtLoginactivityUsername.getText().toString().trim();
String password = mEtLoginactivityPassword.getText().toString().trim();
if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password)) {
ArrayList<User> data = mDBOpenHelper.getAllData();
boolean match = false;
for (int i = 0; i < data.size(); i++) {
User user = data.get(i);
if (name.equals(user.getName()) && password.equals(user.getPassword())) {
match = true;
break;
} else {
match = false;
}
}
if (match) {
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();//销毁此Activity
} else {
Toast.makeText(this, "用户名或密码不正确,请重新输入", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "请输入你的用户名或密码", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
item1activity
package com.example.demo2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class item1Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv_list = (ListView) findViewById(R.id.lv_list);
lv_list.setAdapter(new MyAdapter());
}
private class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return 5;//返回值决定ListView展示多少条数据
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
view = View.inflate(item1Activity.this, R.layout.item1, null);
} else {
view = convertView;
}
return view;
}
}
}