如何保存信息并使其显示在Android应用程序的屏幕上?
我为我的Android应用程序编写了以下代码,我使用SharedReferences编写了这些代码。我从stackexchange获取了所有信息,但仍然无法获取保存的值以显示在屏幕上。这是我的密码:如何保存信息并使其显示在Android应用程序的屏幕上?,android,android-layout,Android,Android Layout,我为我的Android应用程序编写了以下代码,我使用SharedReferences编写了这些代码。我从stackexchange获取了所有信息,但仍然无法获取保存的值以显示在屏幕上。这是我的密码: package com.example.shivamgandhi.gyrosafe; import android.content.Intent; import android.os.Bundle; import android.content.Context; import android.su
package com.example.shivamgandhi.gyrosafe;
import android.content.Intent;
import android.os.Bundle;
import android.content.Context;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.app.Activity;
import android.view.MenuItem;
import android.util.Log;
import android.widget.Button;
import android.content.SharedPreferences;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.TextView;
import org.w3c.dom.Text;
public class settings_page_activity extends AppCompatActivity implements View.OnClickListener{
EditText ed1, ed2;
Button btnArray2[] = new Button[5];
public static final String MyPREFERENCES = "MyPrefs";
public static final String Phone = "phoneKey";
public static final String Email = "emailKey";
SharedPreferences sharedpreferences;
SharedPreferences.Editor preferenceEditor;
private static final int PREFERENCE_MODE_PRIVATE = 0;
TextView textView;
TextView textView2;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_page);
btnArray2[0] = (Button)findViewById(R.id.button3);
btnArray2[1] = (Button)findViewById(R.id.button4);
btnArray2[2] = (Button)findViewById(R.id.button5);
btnArray2[3] = (Button)findViewById(R.id.button6);
btnArray2[4] = (Button)findViewById(R.id.button12);
for (int i = 0; i <4; i++){
btnArray2[i].setOnClickListener(this);
}
sharedpreferences = this.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
ed1 = (EditText)findViewById(R.id.editText);
ed2 = (EditText)findViewById(R.id.editText2);
textView = (TextView)findViewById(R.id.textView41);
textView.setText(sharedpreferences.getString("Email", "No Email"));
textView2 = (TextView)findViewById(R.id.textView42);
textView2.setText(sharedpreferences.getString("Phone", "No Phone"));
}
@Override
public void onClick(View v) {
if(v == findViewById(R.id.button3)){
Intent myIntent = new Intent(this, MainActivity.class );
startActivity(myIntent);
}
else if(v == findViewById(R.id.button4)){
Intent myIntent = new Intent(this, MainActivity.class);
startActivity(myIntent);
}
else if(v == findViewById(R.id.button5)){
Intent myIntent = new Intent(this, MainActivity.class);
startActivity(myIntent);
}
else if(v == findViewById(R.id.button6)){
Intent myIntent = new Intent(this, MainActivity.class);
startActivity(myIntent);
}
else if(v == findViewById(R.id.button12)){
String e = ed1.getText().toString();
String ph = ed2.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
if(sharedpreferences.getString("Email", "No Email") != "No Email"){
sharedpreferences.edit().remove("Email").apply();
}
if(sharedpreferences.getString("Phone", "No Phone") != "No Phone"){
sharedpreferences.edit().remove("Phone").apply();
}
editor.putString("Email", e);
editor.putString("Phone", ph);
editor.commit();
Toast.makeText(settings_page_activity.this,"Thanks",Toast.LENGTH_LONG).show();
}
}
}
package com.example.shivamgandhi.gyrosafe;
导入android.content.Intent;
导入android.os.Bundle;
导入android.content.Context;
导入android.support.design.widget.FloatingActionButton;
导入android.support.design.widget.Snackbar;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.Toolbar;
导入android.view.view;
导入android.view.Menu;
导入android.app.Activity;
导入android.view.MenuItem;
导入android.util.Log;
导入android.widget.Button;
导入android.content.SharedReferences;
导入android.widget.EditText;
导入android.widget.Toast;
导入android.widget.TextView;
导入org.w3c.dom.Text;
公共类设置\页面\活动扩展AppCompatActivity实现View.OnClickListener{
编辑文本ed1、ed2;
按钮btnArray2[]=新按钮[5];
公共静态最终字符串MyPREFERENCES=“MyPrefs”;
公共静态最终字符串Phone=“phoneKey”;
公共静态最终字符串Email=“emailKey”;
SharedReferences SharedReferences;
SharedReferences.Editor首选项编辑器;
私有静态最终整数首选项\模式\私有=0;
文本视图文本视图;
文本视图文本视图2;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.settings\u页面);
btnArray2[0]=(按钮)findViewById(R.id.button3);
btnArray2[1]=(按钮)findViewById(R.id.button4);
btnArray2[2]=(按钮)findViewById(R.id.button5);
btnArray2[3]=(按钮)findViewById(R.id.button6);
btnArray2[4]=(按钮)findViewById(R.id.button12);
对于(inti=0;i这看起来像是一个典型的竞争条件。您混合了同步和异步调用来修改共享首选项
editor.commit();
是同步的,这意味着它会立即应用,其中asSharedReferences.apply()
是在后台执行的,因此您对SharedReferences.edit().remove(“电话”).apply();
和SharedReferences.edit().remove(“电子邮件”).apply()的调用很可能会发生调用editor.commit();
如果这不能解决您的问题,那么当您点击按钮12时,您的吐司会出现吗?有可能是v==findViewById(R.id.button12)
的相等性检查失败,在这种情况下,“首选”方法是使用v.getId()进行此检查==R.id.button12
。这很少见,但我听说在直接比较视图时,此相等性检查失败
编辑
另外,应该注意的是,在上面的代码中已经创建了对编辑器的引用之后,您调用了prefs.edit().remove().apply()(稍后还要使用editor.stuff()),这也是冗余编码,应该加以解决,但这与您的实际问题并不密切相关
btnArray2[0]=(按钮)findViewById(R.id.button3);
btnArray2[1]=(按钮)findViewById(R.id.button4);
btnArray2[2]=(按钮)findViewById(R.id.button5);
btnArray2[3]=(按钮)findViewById(R.id.button6);
btnArray2[4]=(按钮)findViewById(R.id.button12);
对于(int i=0;我请发布您的R.layout.settings\u page
代码,这可能有助于澄清您的问题。您的代码看起来不太好。您了解它的每个部分都在做什么吗?如果不了解,您应该后退一步,学习java/android编程。我已经学习了java编程。我已经尝试学习了from的各种来源,但我刚刚发现,通过实际编码和观看教程来学习最适合我。无论出于何种原因,我都不太擅长学习comp sci。我只是想澄清一下:你应该调用commit或apply,每个sharedReference只调用一次。编辑器实例,当所有修改完成后。此外,没有一个ne设置新值之前,请先删除现有值。当值已存在时调用put
将只覆盖现有值。您不需要先删除上一个值。我已修复代码,因此删除了冗余覆盖部分,只调用了一次提交。我尝试修复该按钮,因为TOAST没有出现r当我点击它时,但更改没有帮助。不幸的是,我仍然没有看到TOAST。我已经用另一个修复程序更新了我的答案,解释了为什么你的代码不工作。哇,我不知道我在调试方面有那么差:(。谢谢你的帮助,设置页面按预期工作。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/Settings_Page"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Begin_Calibration"
android:id="@+id/button3"
android:layout_marginTop="31dp"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/View_Baseline_Results"
android:id="@+id/button4"
android:layout_below="@+id/button3"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Test_Design_Summary"
android:id="@+id/button5"
android:layout_alignTop="@+id/button4"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/Email_Results_To"
android:id="@+id/textView2"
android:layout_marginStart="24dp"
android:layout_below="@+id/button4"
android:layout_alignParentStart="true"
android:layout_marginTop="40dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/Emergency_Number"
android:id="@+id/textView3"
android:layout_marginTop="57dp"
android:layout_below="@+id/textView2"
android:layout_alignStart="@+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/editText"
android:layout_below="@+id/textView2"
android:layout_alignStart="@+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/editText2"
android:layout_below="@+id/textView3"
android:layout_alignStart="@+id/textView3" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Home"
android:id="@+id/button6"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@+id/textView"
android:layout_marginStart="26dp"
android:layout_marginBottom="41dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Save_Information"
android:id="@+id/button12"
android:layout_above="@+id/button6"
android:layout_toStartOf="@+id/button5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView42"
android:layout_gravity="right"
android:layout_above="@+id/editText"
android:layout_alignEnd="@+id/editText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/textView43"
android:layout_above="@+id/editText2"
android:layout_toEndOf="@+id/button12" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView41"
android:layout_gravity="right" />
</LinearLayout>
btnArray2[0] = (Button)findViewById(R.id.button3);
btnArray2[1] = (Button)findViewById(R.id.button4);
btnArray2[2] = (Button)findViewById(R.id.button5);
btnArray2[3] = (Button)findViewById(R.id.button6);
btnArray2[4] = (Button)findViewById(R.id.button12);
for (int i = 0; i <4; i++){
btnArray2[i].setOnClickListener(this);
}