基于编辑文本输入的微调器值

时间:2019-07-02 05:51:41

标签: java android android-edittext android-spinner

我创建了一个编辑文本和一个微调器,在该微调器中有多个选项是永久性和临时性。如果我在编辑文本中输入“品牌”,则在微调器中它应该仅显示“永久”而不显示临时,但如果我输入其他任何单词,则应显示永久或临时

我已经尝试过,如果我输入整数,那么它将返回月份名称。     例如:如果我输入6,则在微调器中显示6月。

主要活动代码

        edit = (EditText) findViewById(R.id.editText);
        spinner = (Spinner) findViewById(R.id.spinner);

        String[] months = new DateFormatSymbols().getMonths();


        ArrayAdapter adapter = new ArrayAdapter<String>(this, 
        android.R.layout.simple_spinner_item,months);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new 
        AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, 
            int position, long id)
            {
                edit.setText(String.valueOf(position + 1));
            }


        });

        edit.addTextChangedListener(new TextWatcher()
        {

            @Override
            public void afterTextChanged(Editable s)
            {
                try
                {

                  int nPosition = Integer.parseInt(s.toString());
                    if (nPosition >= 1 && nPosition <= 12)
                    {
                        spinner.setSelection(nPosition - 1);
                    }
                }
                catch(NumberFormatException nfe)
                {

                }
            }
        });

2 个答案:

答案 0 :(得分:1)

尝试这个...

public class Main2Activity extends AppCompatActivity {

Spinner sp1;

ArrayAdapter<String> sAdapter1;
ArrayAdapter<String> sAdapter2;
String s1[] = {"Permanent"};
String s2[] = {"Permanent", "Temporary"};

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

    sp1 = (Spinner) findViewById(R.id.spinner1);
    EditText et = (EditText) findViewById(R.id.editText1);

    sAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, s1);
    sAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    sAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, s2);
    sAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    et.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if (editable.toString().equals("Brand")) {
                sp1.setAdapter(sAdapter1);
            } else {
                sp1.setAdapter(sAdapter2);
            }
        }
    });

}
}

答案 1 :(得分:0)

创建两个数组

String[] array1 = new String[]{"Permanent"};
String[] array2 = new String[]{"Permanent", "Temporary"};

现在,在EditText textChanged侦听器上,根据您的情况使用合适的数组初始化微调器适配器

edit.addTextChangedListener(new TextWatcher(){

            @Override
            public void afterTextChanged(Editable s)
            {
                if(s.toString.equals("BRAND")){
                    spinnerAdapter = new ArrayAdapter(context, android.R.layout.simple_spinner_item, array1)
                }else{
                    spinnerAdapter = new ArrayAdapter(context, android.R.layout.simple_spinner_item, array2)
                }
                spinner.setAdapter(spinnerAdapter);
            }
        });