2016-02-26 113 views
1

我希望創建一個搜索功能,我有一個編輯文本,我已經在它下面放置了一個微調器。當我在edittext中輸入任何東西時,有一個數組用於比較edittext中輸入的字母,並在篩選數組元素後將其添加到微調器中並打開微調器。從spinner刪除焦點

我面臨的問題是,當打開微調框時,edittext會失去焦點,我需要關閉微調以重新開始輸入。

我附上代碼請人誰有任何想法,請幫助我:

佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 

     android:orientation="vertical" 

     > 
     <EditText 
      android:id="@+id/edittext" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:focusableInTouchMode="true" 
      /> 
     <Spinner 
      android:id="@+id/edit_spinner" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:focusableInTouchMode="false" 
      /> 

    </LinearLayout> 
</RelativeLayout> 

活動:

public class MainActivity extends AppCompatActivity { 

    EditText editText; 
    Spinner spinner; 
    ArrayList<String> mylist; 
    String[] strngarray; 
    ArrayAdapter<String> adapter; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mylist = new ArrayList<>(); 
     strngarray = getResources().getStringArray(R.array.planets_array); 
     for(int i=0;i<strngarray.length;i++) 
     { 
      mylist.add(strngarray[i]); 

     } 

     textView = (TextView) findViewById(R.id.tv); 
     editText = (EditText) findViewById(R.id.edittext); 
     spinner = (Spinner) findViewById(R.id.edit_spinner); 
     adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,(List)mylist); 
// Specify the layout to use when the list of choices appears 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
// Apply the adapter to the spinner 
     spinner.setAdapter(adapter); 
     editText.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
       Log.d("TextWatcher","before changed Char : "+s); 
      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       Log.d("TextWatcher", "on changed Char : " + s); 
if(!s.equals("")) { 

    mylist.clear(); 

    for (int i = 0; i < strngarray.length; i++) { 
     String temp = strngarray[i].toLowerCase(); 
     String prefix = s.toString().toLowerCase(); 
     if (temp.startsWith(prefix)) { 
      mylist.add(strngarray[i]); 
     } 

    } 
    if (mylist.size() != 0) { 
     adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, (List) mylist); 
     adapter.notifyDataSetChanged(); 
     spinner.performClick(); 
     spinner.clearFocus(); 
     editText.requestFocus(); 
    } 
} 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       Log.d("TextWatcher","after changed Char : "+s); 
      } 
     }); 

    } 


} 

回答

1

而不是使用一個TextView和微調您的可以實現AutoCompleteTextView。

這允許用戶輸入時的建議。 您可以使用適配器綁定要搜索的數據。

可能會有所幫助的例子可以發現here.