package com.example.pengtuanyuan.logindemo01;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText ed_inputName;
private EditText ed_inputPassword;
private CheckBox cb_choice;
private Button bt_login;
private static Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext=this;
ed_inputName = (EditText) findViewById(R.id.ed_inputName);
ed_inputPassword = (EditText) findViewById(R.id.ed_inputPassword);
cb_choice = (CheckBox) findViewById(R.id.cb_choice);
bt_login = (Button) findViewById(R.id.bt_login);
bt_login.setOnClickListener(this);
Map<String,String> map=UserInfoUtil.getUserInfo_android(mContext);
if (map!=null){
String userName=map.get("useName");
String password=map.get("usePassword");
ed_inputName.setText(userName);
ed_inputPassword.setText(password);
cb_choice.setChecked(true);
}
File sdcard_filedir = Environment.getExternalStorageDirectory();
long usableSpace = sdcard_filedir.getUsableSpace();
long totalSpace = sdcard_filedir.getTotalSpace();
String usableSpace_str = Formatter.formatFileSize(mContext, usableSpace);
String totalSpace_str = Formatter.formatFileSize(mContext, totalSpace);
if(usableSpace <1024*1024*200){
Toast.makeText(mContext,"SD card have not enough space,the rest is :"+usableSpace_str,Toast.LENGTH_SHORT).show();
return;
}
}
private void login(){
String useName = ed_inputName.getText().toString().trim();
String usePassword = ed_inputPassword.getText().toString().trim();
boolean isChecked=cb_choice.isChecked();
if (TextUtils.isEmpty(useName)||TextUtils.isEmpty(usePassword)){
Toast.makeText(mContext,"Name and password cannot be empty",Toast.LENGTH_SHORT).show();
return;
}
if (isChecked){
if(! Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(mContext,"SDcard no be mounted",Toast.LENGTH_SHORT).show();
return;
}
Boolean result=UserInfoUtil.saveUserInfo_android(mContext,useName,usePassword);
if (result){
Toast.makeText(mContext,"Name and password be saved",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(mContext,"Name and password cannot be saved",Toast.LENGTH_SHORT).show();
}
}else {
Toast.makeText(mContext,"Name and password don't need to be saved",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_login:
if((ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE))!= PackageManager.PERMISSION_GRANTED) {
//说明没有动态权限需要申请
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},0);
}else {
//说明有权限直接调用方法
login();
}
break;
default:
break;
}
}
}
package com.example.pengtuanyuan.logindemo01;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import java.util.HashMap;
import java.util.Map;
public class UserInfoUtil {
public static Boolean saveUserInfo_android (Context context,String useName, String usePassword) {
try {
//SharedPreferences sharedPreferences = context.getSharedPreferences("user_info", Context.MODE_PRIVATE);
SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("userName",useName);
editor.putString("usePassword",usePassword);
editor.commit();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static Map<String,String> getUserInfo_android(Context context){
try {
//SharedPreferences sharedPreferences = context.getSharedPreferences("user_info", Context.MODE_PRIVATE);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
String username = sharedPreferences.getString("username", "");
String password = sharedPreferences.getString("password", "");
HashMap<String,String> hashMap=new HashMap<String,String>();
hashMap.put("useName",username);
hashMap.put("usePassword",password);
return hashMap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.pengtuanyuan.logindemo01.MainActivity">
<EditText
android:id="@+id/ed_inputName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/input_name"/>
<EditText
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:id="@+id/ed_inputPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/input_password"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:id="@+id/cb_choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cb_rememberPassword"/>
<Button
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:id="@+id/bt_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bt_login"/>
</RelativeLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pengtuanyuan.logindemo01">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>