张学友

界面布局

*利用LinearLayout线性布局

<?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_wuyue9"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/name" />
    <EditText
        android:id="@+id/age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/age"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/in"
            android:textSize="20sp"
            android:layout_marginRight="40dp"/>

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/read"
            android:textSize="20sp"/>
    </LinearLayout>
</LinearLayout

java的代码的实现

package cn.edu.niit.aaaaaaaaaaaaaaaaaaaa;

        import android.app.Activity;
        import android.content.Context;
        import android.content.SharedPreferences;
        import android.content.SharedPreferences.Editor;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.Toast;

class SharedPreferencesActivity extends Activity
{
    private  EditText name=null;
    private EditText age=null;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wuyue9);
        name=(EditText)findViewById(R.id.name);
        age=(EditText)findViewById(R.id.age);
        Button saveButton=(Button)findViewById(R.id.button1);
        Button restorationButton=(Button)findViewById(R.id.button2);
        //写入信息
        saveButton.setOnClickListener
                (
                        new View.OnClickListener()
                        {
                            @Override
                            public void onClick(View v)
                            {

                                String web_name=name.getText().toString();
                                String true_age=age.getText().toString();
                                SharedPreferences preferences=getSharedPreferences("softinfo",Context.MODE_WORLD_READABLE);
                                Editor edit=preferences.edit();
                                edit.putString("name", web_name);
                                edit.putInt("age",new Integer(true_age));
                                edit.commit();
                                Toast.makeText(SharedPreferencesActivity.this, R.string.success,Toast.LENGTH_LONG).show();

                            }
                        }
                );
        //读取信息
        restorationButton.setOnClickListener
                (
                        new View.OnClickListener()
                        {

                            @Override
                            public void onClick(View v)
                            {
         SharedPreferences ferences=getSharedPreferences("softinfo",0);
         String true_name=ferences.getString("name", "");
        int true_age=ferences.getInt("age", 20);
        name.setText(true_name);
    age.setText(String.valueOf(true_age));
                            }
                        }
                );
    }
}
posted @ 2017-05-09 19:26  孙耀威  阅读(142)  评论(0编辑  收藏  举报