package com.example.iodemo; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import android.R.color; import android.annotation.SuppressLint; import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.graphics.Color; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.Toast; @SuppressLint("ShowToast") public class MainActivity extends Activity { private Button btnSave,btnRead; private EditText etText,etName; private String path, name; private RadioButton rbRed,rbYellow,rbBlue,rbGreen; private int colors=Color.BLUE; /** * IOÁ÷¼¸ºõ³É¶Ô³öÏÖinput output */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init(){ etName=(EditText) findViewById(R.id.editText1); btnSave=(Button) findViewById(R.id.btnSave); btnRead=(Button) findViewById(R.id.btnRead); etText=(EditText) findViewById(R.id.etText); MyListener listener=new MyListener(); btnSave.setOnClickListener(listener); btnRead.setOnClickListener(listener); rbRed=(RadioButton) findViewById(R.id.rbRed); rbYellow=(RadioButton) findViewById(R.id.rbYellow); rbBlue=(RadioButton) findViewById(R.id.rbBlue); rbGreen=(RadioButton) findViewById(R.id.rbGreen); rbListener rblistener=new rbListener(); rbRed.setOnClickListener(rblistener); rbYellow.setOnClickListener(rblistener); rbBlue.setOnClickListener(rblistener); rbGreen.setOnClickListener(rblistener); } class rbListener implements OnClickListener{ public void onClick(View arg0) { // TODO Auto-generated method stub switch (arg0.getId()) { case R.id.rbRed: colors=Color.RED; break; case R.id.rbYellow: colors=Color.YELLOW; break; case R.id.rbBlue: colors=Color.BLUE; break; case R.id.rbGreen: colors=Color.GREEN; break; } } } class MyListener implements OnClickListener{ public void onClick(View arg0) { // TODO Auto-generated method stub switch (arg0.getId()) { case R.id.btnSave: saveData(); saveColor("col",colors); Toast.makeText(MainActivity.this, "±£´æ³É¹¦£¡", 0).show(); break; case R.id.btnRead: String s=readData(); etText.setText(s); int col=getColor("col"); etText.setTextColor(col); break; } } } private void saveColor(String key,int color){ name=etName.getText().toString(); SharedPreferences sh=getSharedPreferences(name, MODE_PRIVATE); Editor editor=sh.edit(); editor.putInt(key, color); editor.commit(); } private int getColor(String key){ name=etName.getText().toString(); SharedPreferences sh=getSharedPreferences(name, MODE_PRIVATE); int col=sh.getInt(key, color.black); if(col==Color.RED) rbRed.setChecked(true); if(col==Color.YELLOW) rbYellow.setChecked(true); if(col==Color.BLUE) rbBlue.setChecked(true); if(col==Color.GREEN) rbGreen.setChecked(true); return col; } private void saveData(){ path=Environment.getExternalStorageDirectory()+"/aa.txt"; FileOutputStream fos=null; OutputStreamWriter osw=null; BufferedWriter bw=null; try { fos=new FileOutputStream(path); osw=new OutputStreamWriter(fos); bw=new BufferedWriter(osw); bw.write(etText.getText().toString()); bw.flush(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { bw.close(); osw.close(); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } private String readData() { // path=Environment.getExternalStorageDirectory()+"/aa.txt"; String str=""; FileInputStream fis=null; InputStreamReader isr=null; BufferedReader br=null; try { fis=new FileInputStream(path); isr=new InputStreamReader(fis); br=new BufferedReader(isr); str=br.readLine(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { br.close(); isr.close(); fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return str; } }
最近下载更多
最近浏览更多
652654393 LV4
11月18日
小妹妹 LV7
2023年6月9日
yerikli LV1
2023年6月8日
xiaoqiaothq LV2
2023年4月2日
LITIANYU084414 LV11
2023年1月13日
微信网友_5976073020362752 LV2
2022年5月27日
萌了个乖乖 LV12
2022年5月20日
cxywt12 LV2
2022年5月20日
wang123999 LV19
2021年12月20日
DDheYUER LV1
2021年12月7日