Android:从用户';谁的输入?

Android:从用户';谁的输入?,android,Android,我想在AlertDialogue中提示用户他的名字,在共享的预引用中保存他的名字,并在文本视图中设置他的名字,这样当他返回时可以看到他的名字。到目前为止,我已经有了对话输入,但我不确定它是否正在保存,因为文本视图显示了类似“Android Widget EditText(9o179…等)”的内容,我做错了什么?代码: String name; //global TextView user; 一旦创建: @Override protected void onCreate(Bundle saved

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

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();
}
使用edittext.getText().toString()从EditText获取文本

public void popUp(){

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

    questionName.setTitle(" Enter your name ");
    EditText input=new EditText(this);

    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){
             name=input.getText().toString(); // see this line. 
             saveName();
        }
    });

    AlertDialog dialog = questionName.create();

    dialog.show();
}

建议您访问,以便对该机制和功能有更深入的了解

也就是说,请允许我向您提供一个关于如何在应用程序中存储值的片段

注意,由于我正在片段中应用SharedReference,您需要做一些小的调整

首先

第二

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

 }    
}

Shijil,它成功了,谢谢,但名称没有保存…我从应用程序中退出并返回,什么都没有…这是另一个问题吗?对不起,我正在学习:)
  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();

 }    
}