Android-显示带有SharedReference值的按钮
我目前正在从事一项从按钮1…按钮8保存文本的活动 但它没有显示按钮的文本。我不明白 主要活动=>Android-显示带有SharedReference值的按钮,android,android-spinner,Android,Android Spinner,我目前正在从事一项从按钮1…按钮8保存文本的活动 但它没有显示按钮的文本。我不明白 主要活动=> public void getName(){ SharedPreferences preferences = getSharedPreferences("sample",0); int a=(preferences.getInt("num",0)); String ab=(preferences.getString("Name","")); if(a==0){
public void getName(){
SharedPreferences preferences = getSharedPreferences("sample",0);
int a=(preferences.getInt("num",0));
String ab=(preferences.getString("Name",""));
if(a==0){
button1.setVisibility(View.GONE);
button1.setVisibility(View.VISIBLE);
button1.setText(ab);
}
if(a==1){
button2.setVisibility(View.GONE);
button2.setVisibility(View.VISIBLE);
button2.setText(ab);
}
if(a==3){
button3.setVisibility(View.GONE);
button3.setVisibility(View.VISIBLE);
button3.setText(ab);
}
}
public void onClick(View v) {
SharedPreferences preferences = getSharedPreferences("Sample", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Name", editText.getText().toString());
editor.putInt("num", myNum);
editor.commit();
Intent intent = new Intent(Setup.this, MainActivity.class);
startActivity(intent);
}
设置活动=>
public void getName(){
SharedPreferences preferences = getSharedPreferences("sample",0);
int a=(preferences.getInt("num",0));
String ab=(preferences.getString("Name",""));
if(a==0){
button1.setVisibility(View.GONE);
button1.setVisibility(View.VISIBLE);
button1.setText(ab);
}
if(a==1){
button2.setVisibility(View.GONE);
button2.setVisibility(View.VISIBLE);
button2.setText(ab);
}
if(a==3){
button3.setVisibility(View.GONE);
button3.setVisibility(View.VISIBLE);
button3.setText(ab);
}
}
public void onClick(View v) {
SharedPreferences preferences = getSharedPreferences("Sample", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Name", editText.getText().toString());
editor.putInt("num", myNum);
editor.commit();
Intent intent = new Intent(Setup.this, MainActivity.class);
startActivity(intent);
}
检查您的首选项名称,您正在使用
getSharedPreferences("Sample", 0);
及
检查您的首选项名称,您正在使用
getSharedPreferences("Sample", 0);
及
不显示按钮的文本
这是因为preferences.getStringName作为默认值返回,可能就是这种情况
除此之外,这些行:
button_.setVisibility(View.GONE);
button_.setVisibility(View.VISIBLE);
这真的没有道理。您可以删除该行:
button_.setVisibility(View.GONE);
不显示按钮的文本
这是因为preferences.getStringName作为默认值返回,可能就是这种情况
除此之外,这些行:
button_.setVisibility(View.GONE);
button_.setVisibility(View.VISIBLE);
这真的没有道理。您可以删除该行:
button_.setVisibility(View.GONE);
首先从getName方法中删除这一行 按钮uu.setVisibilityView.go 然后检查用于保存示例中按钮名称的键,您将使用示例名称保存按钮名称并将其检索为示例。请记住,密钥区分大小写 现在来谈谈另一点,以防您编写关键示例只是为了向我们演示这个问题。在开始之前,我想问一下您在mainActivity中调用方法getName的位置。但无论如何,这是一种安全的方法: 保存按钮名称时,请添加以下行
public void onClick(View v) {
SharedPreferences preferences = getSharedPreferences("Sample", 0);
SharedPreferences.Editor editor = preferences.edit();
if(editText.getText().toString().length()>0){
editor.putString("Name", editText.getText().toString());
editor.putInt("num", myNum);
editor.commit();
Intent intent = new Intent(Setup.this, MainActivity.class);
startActivity(intent);}
}
在getName方法中,只需执行以下操作
public void getName(){
SharedPreferences preferences = getSharedPreferences("sample",0);
int a=(preferences.getInt("num",0));
String ab=(preferences.getString("Name",""));
if(a==0){
button1.setVisibility(View.VISIBLE);
button1.setText(ab);
}
if(a==1){
button2.setVisibility(View.VISIBLE);
button2.setText(ab);
}
if(a==3){
button3.setVisibility(View.VISIBLE);
button3.setText(ab);
}
}
首先从getName方法中删除这一行 按钮uu.setVisibilityView.go 然后检查用于保存示例中按钮名称的键,您将使用示例名称保存按钮名称并将其检索为示例。请记住,密钥区分大小写 现在来谈谈另一点,以防您编写关键示例只是为了向我们演示这个问题。在开始之前,我想问一下您在mainActivity中调用方法getName的位置。但无论如何,这是一种安全的方法: 保存按钮名称时,请添加以下行
public void onClick(View v) {
SharedPreferences preferences = getSharedPreferences("Sample", 0);
SharedPreferences.Editor editor = preferences.edit();
if(editText.getText().toString().length()>0){
editor.putString("Name", editText.getText().toString());
editor.putInt("num", myNum);
editor.commit();
Intent intent = new Intent(Setup.this, MainActivity.class);
startActivity(intent);}
}
在getName方法中,只需执行以下操作
public void getName(){
SharedPreferences preferences = getSharedPreferences("sample",0);
int a=(preferences.getInt("num",0));
String ab=(preferences.getString("Name",""));
if(a==0){
button1.setVisibility(View.VISIBLE);
button1.setText(ab);
}
if(a==1){
button2.setVisibility(View.VISIBLE);
button2.setText(ab);
}
if(a==3){
button3.setVisibility(View.VISIBLE);
button3.setText(ab);
}
}
很好地调试您的代码并指定您的问题。例如,您期望的值有多大?不是editText.getText.toString。首选项是否被覆盖?我们将调试您的代码并指定您的问题。例如,您期望的值有多大?不是editText.getText.toString。首选项被覆盖了吗?谢谢。但我不知道,我保存了按钮1的文本,但当我单击编辑按钮=>button1返回相同的内容时,按钮2被保存。请解释:什么意思,对不起,我没有英文。它的意思是:我想在单击编辑按钮时保存按钮的文本。谢谢。但我不知道,我保存了按钮1的文本,但是当我点击编辑按钮=>button1返回相同的,button2已保存:你的意思是什么,请解释对不起,我没有英文。它的意思是:我想在点击编辑按钮时保存按钮的文本。你在不保存中的意思是什么?对不起,我没有英文,它的意思是没有保存按钮文本。它没有保存在哪里?在onClick中?您检查了id您的编辑文本是否为空?当然,我的意思是“按钮未永久保存”,当我单击“退出”按钮时,它总是会更改您的意思是退出后返回主活动时?您在“未保存”中的意思是什么?抱歉,我没有英文,它表示“未保存”按钮文本。它未保存在哪里?在onClick中?你检查了id你的编辑文本不是空的吗?当然,我的意思是按钮“否”永久保存,当我单击“退出”按钮时它总是更改你的意思是退出后返回主活动时?