2017-04-10 37 views
0

我有一個編輯文本,我們鍵入餐廳名稱並在輸入時我想根據輸入的文本顯示帶有餐廳列表的菜單。但鍵盤顯示彈出式菜單爲: Click here to see image 我想讓我的彈出菜單適合鍵盤上方。 這裏是我的代碼,以顯示彈出式菜單:彈出菜單上方的Android顯示鍵盤

final EditText restaurant = (EditText) rootView.findViewById(R.id.reservation_edit_text_restaurant); 
restaurant.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) { 
     PopupMenu popupMenu = new PopupMenu(getContext(),restaurant); 
     //get all the kitchen name from database 
     List<String> kitchenNameList = getKitchenName(); 
     for (int j = 0; j < kitchenNameList.size(); ++j){ 
      popupMenu.getMenu().add(kitchenNameList.get(j)); 
     } 

     popupMenu.show(); 
    } 

    @Override 
    public void afterTextChanged(Editable editable) { 

    } 
}); 
+1

「但鍵盤顯示在彈出式菜單下方,我希望我的彈出式菜單適合鍵盤以上」這聽起來像是你說它正在做你想做的事。你需要更好地描述你想要的東西 - 可能與你有什麼和你想要什麼的圖片。 –

+0

對不起,我的語法錯誤....請參閱編輯的文章@GabeSechan –

回答

1

相反的EditText的使用AutoCompleteTextView:

<AutoCompleteTextView 
      android:id="@+id/Months" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dip" 
      android:layout_marginRight="10dip" 
      android:textStyle="bold" 
      android:width="250dip" /> 

import android.os.Bundle; 
import android.app.Activity; 
import android.util.Log; 
import android.view.View; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.AutoCompleteTextView; 
import android.widget.Toast; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.AdapterView.OnItemSelectedListener; 

public class AutoCompleteString extends Activity implements OnItemClickListener, OnItemSelectedListener { 


    // Initialize variables 

    AutoCompleteTextView textView=null; 
    private ArrayAdapter<String> adapter; 

    //These values show in autocomplete 
    String item[]={ 
       "January", "February", "March", "April", 
       "May", "June", "July", "August", 
       "September", "October", "November", "December" 
      }; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle bundle) { 
     super.onCreate(bundle); 

     setContentView(R.layout.auto_complete_string); 


     // Initialize AutoCompleteTextView values 

      // Get AutoCompleteTextView reference from xml 
      textView = (AutoCompleteTextView) findViewById(R.id.Months); 

      //Create adapter  
      adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, item); 

      textView.setThreshold(1); 

      //Set adapter to AutoCompleteTextView 
      textView.setAdapter(adapter); 
      textView.setOnItemSelectedListener(this); 
      textView.setOnItemClickListener(this); 


    } 


    @Override 
    public void onItemSelected(AdapterView<?> arg0, View arg1, int position, 
      long arg3) { 
     // TODO Auto-generated method stub 
     //Log.d("AutocompleteContacts", "onItemSelected() position " + position); 
    } 

    @Override 
    public void onNothingSelected(AdapterView<?> arg0) { 
     // TODO Auto-generated method stub 

     InputMethodManager imm = (InputMethodManager) getSystemService(
       INPUT_METHOD_SERVICE); 
      imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); 

    } 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
     // TODO Auto-generated method stub 

     // Show Alert  
     Toast.makeText(getBaseContext(), "Position:"+arg2+" Month:"+arg0.getItemAtPosition(arg2), 
       Toast.LENGTH_LONG).show(); 

     Log.d("AutocompleteContacts", "Position:"+arg2+" Month:"+arg0.getItemAtPosition(arg2)); 

    } 

    protected void onResume() { 
     super.onResume(); 
    } 

    protected void onDestroy() { 
     super.onDestroy(); 
    } 

} 

希望如此,它會幫助你。 Source code and output is here

+1

謝謝@lisha我想要你的建議 –