Quokka

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

SharePreferences

界面布局很简单就不过多赘述了

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/ev_userName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/ev_userName" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    <EditText
        android:id="@+id/ev_age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/ev_password" />
</LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1">
        <Button
            android:layout_width="187dp"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:id="@+id/writer"
            android:text="@string/writer"/>
        <Button
            android:layout_width="204dp"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:id="@+id/read"
            android:text="@string/read"/>

    </LinearLayout>
    

接下来是代码的实现,这些在课堂上都讲了不少了

public class ShareActivity extends AppCompatActivity {
    //定义相关组件
    private EditText evuserName;
    private EditText evage;
    //确定按钮
    private Button writer;
    private Button read;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share);
        //获取这些组件findViewId(让其发生关联)
        evuserName = (EditText) findViewById(R.id.ev_userName);
        evage = (EditText) findViewById(R.id.ev_age);
        writer= (Button)findViewById(R.id.writer);
        //设置按钮(写入)监听事件
        writer.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String userName = evuserName.getText().toString();
        String age = evage.getText().toString();
        //根据情况进行存储,这里需要在OnClick方法外使用一个saveToFile方法,对其进行写入
        if(saveToFile(userName)){
            Toast.makeText( ShareActivity.this,"保存成功" ,Toast.LENGTH_SHORT).show();

        }
    }
});
        //设置按钮(读取)监听事件
        read = (Button)findViewById(R.id.read);
        read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String userName = evuserName.getText().toString();
            String filename = "data.txt";
                String result = "";
                try{
                    //打开指定文件,并读取其数据
                    FileInputStream in = openFileInput( filename );
                    BufferedReader reader = new BufferedReader( new InputStreamReader( in ) );
                    userName = reader.readLine();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                userName = evuserName.getText().toString();
                String age = evage.getText().toString();
                Toast.makeText(ShareActivity.this, "输入姓名"+userName+",年龄:" + age,Toast.LENGTH_SHORT).show();
            }
        });
    }

    private boolean saveToFile(String userName) {
        //1.打开文件
        try {
            FileOutputStream out = openFileOutput("data.txt",  MODE_PRIVATE );
            //2.写入
            BufferedWriter writer = new BufferedWriter( new OutputStreamWriter( out ) );
            writer.write(userName);
            //3.关闭文件输入流
            out.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}

END

posted on 2017-05-09 20:27  Quokka  阅读(159)  评论(0编辑  收藏  举报