2015-06-24 9 views
12

में खोज बार की तरह एंड्रॉइड येलप मैं एंड्रॉइड एप्लिकेशन बनाने की कोशिश कर रहा हूं जो खोज करते समय कई पैरामीटर लेता है। वर्तमान में, मेरे पास एक खोजने योग्य इंटरफ़ेस है और मेरी एक्शन बार से खोजने में सक्षम हूं। हालांकि, मैं खोज पट्टी की तरह एक yelp बनाना चाहते हैं के रूप में छवि में दिखाया गया है, जहां मैं डेटा में प्रवेश करने के लिए 2 पाठ फ़ील्ड है:एक्शनबार

enter image description here

कैसे खोज करते समय मुझे एक अतिरिक्त पाठ क्षेत्र में जोड़ सकते हैं?

<item 
    android:id="@+id/action_search" 
    android:title="@string/search" 
    android:orderInCategory="100" 
    impulz:showAsAction="always" 
    impulz:actionViewClass="android.widget.SearchView"/> 

<item android:id="@+id/action_settings" android:title="@string/action_settings" 
    android:orderInCategory="100" impulz:showAsAction="never" /> 

आपको बहुत बहुत धन्यवाद: यहाँ कोड मैं खोज

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_home, menu); 

    // Associate searchable configuration with the SearchView 
    SearchManager searchManager = 
      (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
    final SearchView searchView = 
      (SearchView) menu.findItem(R.id.action_search).getActionView(); 
    searchView.setSearchableInfo(
      searchManager.getSearchableInfo(getComponentName())); 

    // set query change listener to hide keyboard after input has been 
    // submitted 
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 
     @Override 
     public boolean onQueryTextChange(String newText) { 
      return false; 
     } 

     @Override 
     public boolean onQueryTextSubmit(String query) { 

      // hides and then unhides search tab to make sure keyboard 
      // disappears when query is submitted 
      searchView.clearFocus(); 
      return false; 
     } 
    }); 

और यहाँ मेरी menu_layout है initalize लिए उपयोग कर रहा हूँ है।

उत्तर

1

आप actionBar.setCustomView(R.layout.custom_search); का उपयोग करें और actionBar प्रोग्राम के रूप में "कस्टम दृश्य"

इस तरह सेट कर सकते हैं:

custom_search.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="right" 
    android:orientation="horizontal"> 
<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"    
    android:orientation="vertical" > 
    <com.actionbarsherlock.widget.SearchView 
     android:id="@+id/search1" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:iconifiedByDefault="false" 
     android:visibility="invisible" /> 
    <com.actionbarsherlock.widget.SearchView 
     android:id="@+id/search2" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:iconifiedByDefault="false" 
     android:visibility="invisible" /> 
    </LinearLayout> 
    <ImageButton 
     android:id="@+id/btn_search_content" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:src="@drawable/search_content" /> 

</LinearLayout> 

menu.xml

<item 
    android:id="@+id/menu_search" 
    android:icon="@drawable/red_icon" 
    android:showAsAction="always" 
    android:title="search" /> 

उपयोग setCustomView अपने onCreate()

MainActivity में

@Override 
public void onCreate(Bundle savedInstanceState) { 
    // ... 
    actionBar.setCustomView(R.layout.custom_search); 
    actionBarCustomView = action_bar.getCustomView(); 
    searchView = ((SearchView)  actionBarCustomView.findViewById(R.id.search1)); 
    searchView2 = ((SearchView) actionBarCustomView.findViewById(R.id.search2)); 
    searchView.setOnCloseListener(new SearchView.OnCloseListener() { 
     @Override 
     public boolean onClose() { 
      searchView.setVisibility(View.INVISIBLE); 
      searchView2.setVisibility(View.INVISIBLE); 
      return false; 
     } 
    }); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 

     case R.id.menu_search: 
      //set visibilty 
      //do what ever you want 
      break; 

    } 
    return true; 
} 

आशा यह आपके लिए बाहर काम करेंगे।

संबंधित मुद्दे