英文:
Android: shared preferences gives unknown text
问题
以下是翻译好的内容:
所以我需要我的应用程序保存并记住用户在搜索字段中输入的名称。
我使用共享首选项来实现这一点。
但是,当我关闭应用程序并重新打开时,保存的名称不是我得到的文本,而是像这样的文本:
android.support.AppCompatEditText{1f4bcb VEFD... Cl .F...197,800-860,927 app:id/username}
我做错了什么?
static SharedPreferences sharedPref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
namefield = (EditText) findViewById(R.id.username); //这是用户输入字段
Tname = (TextView) findViewById(R.id.NameLabel); //这个标签将在用户按下UPDATE按钮时成为用户名
UPDATEbtn = (Button)findViewById(R.id.Update_btn);
// 从SharedPreferences获取
sharedPref = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
String USERname = sharedPref.getString("name", String.valueOf(0));
Tname.setText(USERname);
}
// 这是我的UPDATE按钮的onClick方法
public void UpdateInfo(View view)
{
Tname.setText(namefield.getText());
sharedPref = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("name", String.valueOf(namefield));
// 应用编辑!
editor.apply();
}
英文:
So i need my app to save and remember the name that user enters in a search field.
I use shared preferences to do that..
But when i close the app and open again, instead of the saved name i get a text like this:
android.support.AppCompatEditText{1f4bcb VEFD..Cl .F...197,800-860,927 app:id/username}
What am doing wrong?
static SharedPreferences sharedPref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
namefield = (EditText) findViewById(R.id.username); //this is the user-input field
Tname = (TextView) findViewById(R.id.NameLabel); //this label will be the username when user press the UPDATEbtn
UPDATEbtn = (Button)findViewById(R.id.Update_btn);
// Get from the SharedPreferences
sharedPref = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
String USERname = sharedPref.getString("name", String.valueOf(0));
Tname.setText(USERname);
}
//this is the onClick method of my UPDATEbtn
public void UpdateInfo(View view)
{
Tname.setText(namefield.getText());
sharedPref = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("name", String.valueOf(namefield));
// Apply the edits!
editor.apply();
}
答案1
得分: 1
你正在尝试访问编辑框。
尝试:
editor.putString("name", namefield.getText().toString());
英文:
You are trying to access the edittext.
Try
editor.putString("name", namefield.getText().toString());
答案2
得分: 1
Sure, here's the translation:
将这一行替换为:
editor.putString("name", namefield.getText().toString());
实际上你没有获取到文本框的内容。
英文:
replace this line
editor.putString("name", String.valueOf(namefield));
to
editor.putString("name", namefield.getText().toString());
Actually you were not getting text of text field.
答案3
得分: 0
你应该获取编辑视图文本并保存。
// 将这行代码修改为
editor.putString("name", String.valueOf(namefield.getText()));
英文:
you should get edit view text and save that
//change this line
editor.putString("name", String.valueOf(namefield));
like below
editor.putString("name", String.valueOf(namefield.getText()));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论