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

时间:2016-11-29 18:33:18

标签: android

我想在AlertDialogue中提示用户他的名字,将他的名字保存在共享的Prefferences上,并在文本视图中设置他的名字,这样当他返回时他可以看到他的名字。到目前为止,我有对话输入,但我不确定它是否正在保存,因为文本视图显示类似于" Android Widget EditText(9o179 ......等)"我究竟做错了什么?代码:

String name; //global
TextView user;

OnCreate中:

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

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

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中调用的首选项方法:

 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();
}

2 个答案:

答案 0 :(得分:1)

建议您访问Android Studio Storage Option以更好地了解机制和功能

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

请注意,您需要进行微调,因为我在片段中应用了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();

 }    
}

答案 1 :(得分:0)

使用<select id="carYear" required> <option value="">Select year</option> <option ngFor="year in years">{{year}}</option> </select> 从EditText获取文字,

edittext.getText().toString();