gpt4 book ai didi

java - 在android studio的本地存储中保存小串数据

转载 作者:行者123 更新时间:2023-11-30 00:28:16 27 4
gpt4 key购买 nike

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

--------------------------------第一个 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);
}
}
);

}
}

----------------------------第二个 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>

谢谢

最佳答案

如果您要保存的键值集合相对较小,则应使用 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", "");

关于java - 在android studio的本地存储中保存小串数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44972613/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com