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

标签 android

我想在 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/

相关文章:

android - 将月球着陆器转换为使用 Opengl

android - 使用 Facebook SDK 4.0.1 膨胀类 com.facebook.widget.LoginButton 时出错

Android Facebook GraphRequest 不返回电子邮件

android - 简单的 Haxe (HaxePunk) 游戏在 HTC Desire (Icecream Sandwich) 上运行速度太慢

android - 如何请求振动许可?

android - 如何在android中调试plus client(google+) "internal error"toast?

android - 在 build.gradle 中启用 proguard 的 Linphone Android 的 Proguard 规则

Android Map 是纯白屏 - 没有错误

java - 如何对 Telegram api 进行 RPC 调用?

java - Google Play 游戏服务仅显示个人资料名称的首字母