import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText editText;
    private TextView textView;
    private Button write;
    private Button read;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        write.setOnClickListener(this);
        read.setOnClickListener(this);
    }

    private void initView() {
        editText=(EditText)findViewById(R.id.Edit_Test);
        textView=(TextView)findViewById(R.id.Text_Test);
        write=(Button)findViewById(R.id.write);
        read=(Button)findViewById(R.id.read);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.write:
                String some=editText.getText().toString();
                SharedPreferences pref = MainActivity.this.getSharedPreferences("data",MODE_PRIVATE);
                SharedPreferences.Editor editor = pref.edit();
                editor.putString("Content",some);
                editor.commit();
                Toast.makeText(MainActivity.this, "写入成功" , Toast.LENGTH_LONG).show();
                editText.setText("");
                break;
            case R.id.read:
                SharedPreferences pre = getSharedPreferences("data",MODE_PRIVATE);
                String name = pre.getString("Content","");
                textView.setText(name);
                Toast.makeText(MainActivity.this, "读取成功" , Toast.LENGTH_LONG).show();
                break;

        }
    }
}