11.12周安卓作业
1
<?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"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="123" />
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写入"/>
<EditText
android:id="@+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="123" />
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:id="@+id/et_in"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="输入你想要写入的内容"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.042" />
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginTop="120dp"
android:onClick="click1"
android:text="写入"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.365"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.154" />
<EditText
android:id="@+id/et_out"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="显示读取的内容"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.421" />
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:onClick="click2"
android:text="读取"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.346"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.541" />
</androidx.constraintlayout.widget.ConstraintLayout>
2.
<?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"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:background="#27D52C"
android:padding="5dp"
android:text="用户登录"
android:textColor="#FFFFFF"
android:textSize="25sp" />
<LinearLayout
android:id="@+id/ll_1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="账号"
android:textColor="#000000"
android:textSize="18sp" />
<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="请输入账号" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="密码"
android:textColor="#000000"
android:textSize="18sp" />
<EditText
android:id="@+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="请输入密码" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_3"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginLeft="25dp"
android:orientation="horizontal">
<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="记住密码" />
<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="自动登录" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="登录" />
</LinearLayout>
</LinearLayout>
package com.example.myapplication;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.CheckResult;
public class MainActivity extends Activity implements android.view.View.OnClickListener , CompoundButton.OnCheckedChangeListener {
private EditText et_account;
private EditText et_password;
private Button btn_login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
Map<String, String> userInfo=qq.getUserInfo(this);
if(userInfo!=null){
et_account.setText(userInfo.get("account"));
et_password.setText(userInfo.get("password"));
}
}
private void initView() {
// TODO Auto-generated method stub
et_account=(EditText)findViewById(R.id.user);
et_password=(EditText)findViewById(R.id.password);
btn_login=(Button)findViewById(R.id.b1);
btn_login.setOnClickListener(this);
CheckBox cb1=findViewById(R.id.cb1);
cb1.setOnCheckedChangeListener(this);
CheckBox cb2=findViewById(R.id.cb2);
cb2.setOnCheckedChangeListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.b1:
String account=et_account.getText().toString().trim();
String password=et_password.getText().toString();
if(TextUtils.isEmpty(account)){
Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(password)){
Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
switch (compoundButton.getId()){
case R.id.cb1 :
String account=et_account.getText().toString().trim();
String password=et_password.getText().toString();
boolean isSaveSuccess=qq.saveUserInfo(this, account, password);
if(isSaveSuccess&&b){
Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
et_account.setText(null);
et_password.setText(null);
}
break;
case R.id.cb2: account=et_account.getText().toString().trim();
password=et_password.getText().toString();
if(TextUtils.isEmpty(account)){
Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(password)){
Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
return;
}
if(b)
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
else{
Toast.makeText(this, "请登录", Toast.LENGTH_SHORT).show();
}
}
}
复制代码
复制代码
package com.example.myapplication;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
public class qq {
public static boolean saveUserInfo(Context context,String account,String password ){
SharedPreferences sp=context.getSharedPreferences("data", context.MODE_PRIVATE);
SharedPreferences.Editor editor=sp.edit();
editor.putString("Username", account);
editor.putString("pwd", password);
editor.commit();
return true;
}
public static Map<String, String> getUserInfo(Context context){
SharedPreferences sp=context.getSharedPreferences("data", context.MODE_PRIVATE);
String account=sp.getString("Username", null);
String password=sp.getString("pwd", null);
Map<String, String> userMap=new HashMap<String,String>();
userMap.put("account", account);
userMap.put("password", password);
return userMap;
}
}
package com.example.myapplication;
import android.content.Context;
import android.content.SharedPreferences;
import java.util.HashMap;
import java.util.Map;
public class saveQQ {
public static boolean SaveUserInfo(Context context, String account, String password) {
SharedPreferences sp = context.getSharedPreferences("data", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.putString("account", account);
edit.putString("pwd", password);
edit.commit();
return true;
}
public static Map<String,String>getUserInfo(Context context){
SharedPreferences sp=context.getSharedPreferences("data",Context.MODE_PRIVATE);
String account=sp.getString("account",null);
String password=sp.getString("pwd",null);
Map<String,String>userMap=new HashMap<String,String>();
userMap.put("account",account);
userMap.put("password",password);
return userMap;
}
}