gpt4 book ai didi

Android:保存和读取用户输入的数据?

转载 作者:行者123 更新时间:2023-11-30 00:52:03 25 4
gpt4 key购买 nike

我想在 AlertDialogue 中提示用户他的名字,将他的名字保存在共享首选项中,并在 TextView 中设置他的名字,以便当他返回时可以看到他的名字。到目前为止,我有对话输入,但我不确定它是否正在保存,因为 TextView 显示类似“Android Widget EditText(9o179...etc)”的内容,我做错了什么?代码:

String name; //global
TextView user;

创建:

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

buttonCreate();
preferences();
togglePlay();
}

切换播放方法:

public void togglePlay() {
init.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

if (isChecked) {
KeysOn();

} else {

KeysOff();
}
}
});
}

public void KeysOn() {
Toast.makeText(getApplicationContext(), getString(R.string.ToastToggleOn), Toast.LENGTH_SHORT).show();
user.setText(name);
//etc
}

在onCreate中调用的preferences方法:

 public void preferences(){


SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());


name= settings.getString("name",name);
saveName();

data = settings.getString("stage", "Indoors");
String backGround = settings.getString("stage", "Indoors");

if (backGround.equals("Indoors")) {
Picasso.with(this).load(R.drawable.shocked_crowd).fit().centerCrop().into(palco);
Toast.makeText(getApplicationContext(), getString(R.string.stageIndoors), Toast.LENGTH_LONG).show();


}
if (backGround.equals("Street")) {
Picasso.with(this).load(R.drawable.coins).fit().centerCrop().into(palco);
Toast.makeText(getApplicationContext(), getString(R.string.stageStreet), Toast.LENGTH_LONG).show();
}
}

编辑:缺少代码:

public void popUp(){

AlertDialog.Builder questionName = new AlertDialog.Builder(this);

questionName.setTitle(" Enter your name ");
EditText input=new EditText(this);
name=input.toString();
LinearLayout.LayoutParams lp= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
questionName.setView(input);
questionName.setNegativeButton(" cancel ", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){

}
});

questionName.setPositiveButton(" done ", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){

}
});

AlertDialog dialog = questionName.create();

dialog.show();
}
public void saveName(){
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor editor = settings.edit();
editor.putString("name", name);
editor.commit();
}

最佳答案

推荐您访问Android Studio Storage Option扩大对机制和功能的认识

话虽如此,请允许我向您提供一段我如何在我的应用程序中存储值的 fragment

请注意,您需要进行一些小的调整,因为我在一个 fragment 中应用了 Sharedpreferences

首先

   addusername = (EditText) oView.findViewById(R.id.addnewUsername);

第二个

  //adjustment and reattachment
String bonsiour = addusername.getText().toString().trim();

SharedPreferences sPref = getActivity().getPreferences(0);
SharedPreferences.Editor edt = sPref.edit();
edt.putString("key1", bonsiour);
edt.commit();


//toast to confirm value has been saved
String buzo = sPref.getString("key1", "empty");
Toast.makeText(getActivity(), "You're " + buzo + "!!", Toast.LENGTH_LONG).show();

这是从中提取/读取的方法

  SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String name = prefs.getString("key1", "No name defined");

if(name.equals("PDF_FOUND")){
Toast.makeText(Controller.this,"IT WORKED !", Toast.LENGTH_SHORT).show();




} else{
Toast.makeText(Controller.this,"BAD NEWS !", Toast.LENGTH_SHORT).show();

}
}

关于Android:保存和读取用户输入的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40873228/

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