如何根据EditText输入动态更改微调器值

时间:2014-04-29 19:14:52

标签: android android-spinner

我有EditText editValue;Spinner spinnerActions

我想根据用户在Spinner

中插入的输入,以{din}方式设置适配器EditText

e.g。

if(editValue.getText().equals("something"){
    spinnerActions.setAdapter(adapter1);
}
else if(editValue.getText().equals("something"){
    spinnerActions.setAdapter(adapter2);
}
else{
    //show warning if the user try to select a value of the spinner
}

我怎么能这样做?

PS 对于所有适配器,第一个值是相同的,如果是相关的

2 个答案:

答案 0 :(得分:1)

我会在一分钟后尝试发布更完整的答案。首先,您需要实现一旦获得视图,您需要设置一个侦听器来检查文本中的更改

EditText editText = (EditText) findViewById(R.id.edit_text_id);
editText.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        // TODO use this to set your new string value
        editValue = s;     // this won't work directly, but it is the idea of what you want to accomplish
    }
}

然后,您可以相应地设置适配器,例如(从上面)

if(editValue.equals("something"))
    spinnerActions.setAdapter(adapter1);

答案 1 :(得分:0)

您需要将Adapter设置为spinner,并根据输入文字更改Adapter

中的数据
  final Spinner spinner = (Spinner) findViewById(R.id.spinner);
  final EditText editText = (EditText) findViewById(R.id.editText1);
  Button button = (Button) findViewById(R.id.submit);

  final List<String> stringList = new ArrayList<String>();
  final ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(ListEditActivity.this,
                android.R.layout.simple_spinner_item);

  spinner.setAdapter(spinnerAdapter);

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // clear the array list
                    stringList.clear();
                    // add the default item at first
                    stringList.add("FIRST ITEM");

                    if (editText.getText() != null && editText.getText().length() > 0) {
                        String input = editText.getText().toString();

                        if (input.toLowerCase().equalsIgnoreCase("one")) {
                            // add the spinner items for this input
                            stringList.add("ONE");
                        } else if (input.toLowerCase().equalsIgnoreCase("two")) {
                            // add the spinner items for this input
                            stringList.add("TWO");
                        } else {
                            // show dialog that invalid input
                            return;
                        }
                        // update the adapter with new data
                        spinnerAdapter.clear();
                        // adding the item will also notify the spinner to refresh the list
                        spinnerAdapter.addAll(stringList);
                    }
                }
            });