关于ShareAdapter的使用

原生布局是















Java的代码

package com.example.kimdemon.share;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;



public class MainActivity extends AppCompatActivity {
private EditText yf_name;
private EditText yf_age;
private Button yf_button1;
private Button yf_button2;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
yf_name = (EditText)findViewById(R.id.yf_name);
yf_age = (EditText)findViewById(R.id.yf_age);
yf_button1 = (Button)findViewById(R.id.yf_button1);
yf_button2 = (Button)findViewById(R.id.yf_button2);



setContentView(R.layout.activity_main);
}

private void save(String name,String age){
SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
editor.putString("name",name);
editor.putString("age",age);
editor.commit();
editor.clear();
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_LONG).show();

}

private void read(){
SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
String yf_name = pref.getString("name","");
String yf_age = pref.getString("age","");
Toast.makeText(MainActivity.this,"我叫"+yf_name+"今年"+yf_age,Toast.LENGTH_LONG).show();
}

public void onClick(View view){
switch (view.getId()){
case R.id.yf_button1:
String  name = yf_name.getText().toString();
String age = yf_age.getText().toString();
save(name,age);
break;
case R.id.yf_button2:
read();
break;
}
}
}

你可能感兴趣的:(关于ShareAdapter的使用)