MainActivity.java
package com.example.listpopupwindowtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListPopupWindow;
public class MainActivity extends AppCompatActivity {
String[] strList = {"AIUEO","Kakikukeko","SA Shi Su Se So"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText et_1 = (EditText)findViewById(R.id.et_1);
final ListPopupWindow lpw = new ListPopupWindow(this);
lpw.setAdapter(new ArrayAdapter<String>(
this,android.R.layout.simple_list_item_1,strList));
lpw.setAnchorView(et_1);
lpw.setModal(true);
et_1.setOnTouchListener(new View.OnTouchListener() {
final int DRAWABLE_RIGHT = 2;
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_UP){
if(event.getX()>=v.getWidth()-((EditText) v)
.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width()){
lpw.show();
((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(v.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
return true;
}
}
return false;
}
});
lpw.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
et_1.setText(strList[position]);
lpw.dismiss();
}
});
}
}
Téléchargez le fichier de ressources ci-dessous et copiez core / res / res / drawable-hdpi / numberpicker_down_normal_holo_light.png dans le dossier drawable. https://android.googlesource.com/platform/frameworks/base/+/efd1c67
Spécifiez le fichier image copié avec drawableRight de EditText.
<EditText
android:id="@+id/et_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableRight="@drawable/numberpicker_down_normal_holo_light"/>
Recommended Posts