Android将选定的微调项目设置到编辑文本视图

时间:2014-02-13 04:46:15

标签: android android-spinner

我是android新手。我在我的微调器中显示5个字符串,我希望当用户选择一个字符串时,它会显示在编辑文本视图中。我完成了设置微调器项目,但我不知道如何在编辑文本视图中显示它。我也希望将它保存到共享首选项(我允许用户从微调器中选择文本或手动输入任何文本。我可以将自定义文本设置为共享首选项但不知道如何保存所选文字)

  Spinner spinner;
        String[] alert = { "I am in danger", "Help Me", "Watch Out", "Look For Me",
                "Cover ME" };
        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         SharedPreferences sp = getSharedPreferences("EditValue" ,0);
            String myvalue = sp.getString("myvalue","");
            if(myvalue!=null || !myvalue.equalsIgnoreCase("")) {
               message.setText(myvalue);
            } 


        spinner = (Spinner) findViewById(R.id.Spin_alert_message);
        ArrayAdapter<String> adp = new ArrayAdapter<String>(
                getApplicationContext(), android.R.layout.simple_spinner_item,
                alert);
        spinner.setAdapter(adp);

        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long id) {
            String   selectedItem = alert[position];
                    message.setText(selectedItem);


                    SharedPreferences sp = getSharedPreferences("EditValue" ,0); 
                    SharedPreferences.Editor spEdt = sp.edit();
                    spEdt.putString("myvalue", message.getText().toString());
                    spEdt.commit();




            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });

        // for saving text that user can change as per need
        final SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(this);

        message = (EditText) findViewById(R.id.et_message);
        // loads the text that has been stored to SP and set it to Edit Text
        message.setText(preferences.getString("autoSave", ""));
        // adding addTextChangedListner() to the Edit Text View
        message.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                // saving text after it is changed by the user
                preferences.edit().putString("autoSave", s.toString()).commit();

            }
        });

5 个答案:

答案 0 :(得分:3)

您可以使用spinner setOnItemClickListener,如下所示

spinner.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                String selected = arg0.getItemAtPosition(arg2).toString();
                    edittext.setText(selected);
            }
        });

答案 1 :(得分:1)

onItemSelected方法中,以@Mayuri提到的方式获取所选项目的位置,或

String selected = alert[position];
editText.setText(selected);

答案 2 :(得分:1)

你可以这样:

   spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
   @Override
   public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
    selectedItem = alert[position];
    edittext.setText(selectedItem);


    SharedPreference sp = getSharedPreference("EditValue" ,0); 
    SharedPreference.Editor spEdt = sp.edit();
    spEdt.putString("myvalue", edittext.getText().toString());
    spEdt.commit();


   }

   @Override
   public void onNothingSelected(AdapterView<?> arg0) {
   }
   });

现在,只要您想从SharedPreference获取价值,您就可以获得......

    SharedPreference sp = getSharedPreference("EditValue" ,0);
    String myvalue = sp.getString("myvalue","");
    Log.v("GetValue",myvalue);

<强>更新

在oncreate()方法上检查..

   SharedPreference sp = getSharedPreference("EditValue" ,0);
   String myvalue = sp.getString("myvalue","");
   if(myvalue!=null || !myvalue.equalsIgnoreCase("")) {
      edittext.setText(myvalue);
   } 

<强> NEWUPDATE:

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

    message = (EditText) findViewById(R.id.et_message);
    spinner = (Spinner) findViewById(R.id.Spin_alert_message);

     SharedPreferences sp = getSharedPreferences("EditValue" ,0);
     String myvalue = sp.getString("myvalue","");
        if(myvalue!=null || !myvalue.equalsIgnoreCase("")) {
           message.setText(myvalue);
        } 



    ArrayAdapter<String> adp = new ArrayAdapter<String>(
            getApplicationContext(), android.R.layout.simple_spinner_item,
            alert);
    spinner.setAdapter(adp);

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int position, long id) {
        String   selectedItem = alert[position];
                message.setText(selectedItem);


                SharedPreferences sp = getSharedPreferences("EditValue" ,0); 
                SharedPreferences.Editor spEdt = sp.edit();
                spEdt.putString("myvalue", message.getText().toString());
                spEdt.commit();




        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });


    // loads the text that has been stored to SP and set it to Edit Text

    message.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            // saving text after it is changed by the user


        }
    });

答案 3 :(得分:0)

为微调器设置并实施OnItemSelectedListener(),并在onItemSelected方法内获取所选项目并设置EditText.setText()

答案 4 :(得分:0)

以这种方式实施: OnItemSelectedListener()

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
    selectedItem = alert[position];
    edittext.setText(selectedItem);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});