<?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:gravity="center_horizontal" android:orientation="vertical" tools:context="com.studying.myapplication.MainActivity"> <!--用户名--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="用户名" /> <EditText android:id="@+id/username" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="4" /> </LinearLayout> <!--密码--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="密码" /> <EditText android:id="@+id/passward" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="4" android:inputType="textPassword" /> </LinearLayout> <!--是否记住用户名--> <CheckBox android:id="@+id/remember" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false" android:text="记住用户名" /> <!--登录--> <Button android:id="@+id/login" android:layout_width="200dp" android:layout_height="35dp" android:text="登录" android:textSize="12sp" /> </LinearLayout>
public class MainActivity extends Activity implements View.OnClickListener {
private SharedPreferences mPref;
private SharedPreferences.Editor mEditor;
private EditText mUserName;
private EditText mPassword;
private CheckBox mIsRemember;
private Button mLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
mUserName = (EditText) findViewById(R.id.username);
mPassword = (EditText) findViewById(R.id.passward);
mIsRemember = (CheckBox) findViewById(R.id.remember);
mLogin = (Button) findViewById(R.id.login);
mLogin.setOnClickListener(this);
mPref = getSharedPreferences("user_data", MODE_PRIVATE);
mEditor = mPref.edit();
//若之前曾设置过记住用户名,则读取并设置用户名
if (mPref.getBoolean("is_remember", false)) {
mUserName.setText(mPref.getString("user_name", ""));
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.login:
String userName = mUserName.getText().toString().trim();
String password = mPassword.getText().toString().trim();
//测试用账号
if ("admin".equals(userName) && "123456".equals(password)) {
Toast.makeText(this, "登录成功!", Toast.LENGTH_SHORT).show();
//若勾选了记住用户名,则保存数据
if (mIsRemember.isChecked()) {
mEditor.putString("user_name", userName);
mEditor.putBoolean("is_remember", true);
mEditor.commit();
}
} else {
Toast.makeText(this, "用户名或密码错误!", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有