在android studio上保存本地存储上的小数据串

时间:2017-07-07 13:54:42

标签: java android save

我正在尝试制作一个提醒用户接种疫苗的项目。我已经取得了一些进展,并使用EditText创建了出生日期表单。我似乎无法找到如何将这些字符串保存到文件中,因此当用户关闭应用程序时,它们会被保存。这是代码。有两项活动。我正在努力保存第二项活动中的dita1 muaji 1和viti1。

--------------------------------- First Activity ------------ -------------------------------------------------- --------------------

package al.programming.erlisciko.vaksinimi;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MyVaccines extends AppCompatActivity {

    public Button vendosni_ditelindjen;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_vaccines);
        OnClickButtonListener();
    }

    public void OnClickButtonListener(){
        vendosni_ditelindjen = (Button)findViewById(R.id.vendosni_ditelindjen);
        vendosni_ditelindjen.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent set = new Intent ("al.programming.erlisciko.vaksinimi.SetDate");
                                startActivity(set);
                    }
                }
        );

    }
}

---------------------------- Second Activity ----------------- --------------------

package al.programming.erlisciko.vaksinimi;

import android.app.DatePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;



public class SetDate extends AppCompatActivity {
    public Button ruani;
    public EditText dita, muaji, viti;
    public String dita1, muaji1, viti1;



    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_date);


        ruani = (Button)findViewById(R.id.ruani);
        dita   = (EditText)findViewById(R.id.dita);
        muaji   = (EditText)findViewById(R.id.muaji);
        viti  = (EditText)findViewById(R.id.viti);

        ruani.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                Log.v("dita1", dita.getText().toString());
                Log.v("muaji1", muaji.getText().toString());
                Log.v("viti1", viti.getText().toString());
            }
        });
        System.out.print(dita1);

    }
}

------------------------------- XML 1 -------------- ------------------------------

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="al.programming.erlisciko.vaksinimi.MyVaccines">

    <Button
        android:id="@+id/vendosni_ditelindjen"
        android:layout_width="368dp"
        android:layout_height="wrap_content"
        android:text="Vendosni Ditëlindjen"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="0.481"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.984" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ju lutem vendosni ditëlindjen"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent" />

</android.support.constraint.ConstraintLayout>

-------------------------------- XML 2 ------------- ------------------------------

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="al.programming.erlisciko.vaksinimi.SetDate">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Vendosni Ditëlindjen"
        android:textAlignment="center"
        android:textSize="24sp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="16dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="0.497" />

    <EditText
        android:id="@+id/dita"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Dita"
        android:inputType="date"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.229" />

    <EditText
        android:id="@+id/muaji"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Muaji"
        android:inputType="date"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.431" />

    <EditText
        android:id="@+id/viti"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Viti"
        android:inputType="date"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.647" />

    <Button
        android:id="@+id/ruani"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:text="Ruani Datën"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.914" />

</android.support.constraint.ConstraintLayout>

谢谢

1 个答案:

答案 0 :(得分:1)

如果您要保存的关键值集合相对较少,则应使用SharedPreferences

写入共享首选项

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("Name", yourname);
editor.commit();

从共享偏好设置中阅读

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String name = sharedPref.getString("Name", "");