import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
public class MainActivity extends Activity {
private MainActivity self;
private EditText et_zh;
private ImageButton ib_zh;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
self = this;
et_zh = (EditText) findViewById(R.id.et_zh);
ib_zh = (ImageButton) findViewById(R.id.ib_zh);
ib_zh.setOnClickListener(new OnClickListener() {
private ListView listView;
private PopupWindow popLogin;
private QAdapter adapter;
public void onClick(View v) {
if (popLogin == null) {
InputMethodManager imm = (InputMethodManager) getSystemService(self.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(et_zh.getWindowToken(), 0);
if (adapter == null) {
List users = new ArrayList();
users.add("name1");
users.add("name2");
users.add("name3");
adapter = new QAdapter(self, users, et_zh);
listView = new ListView(self);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
listView.setLayoutParams(llp);
listView.setFooterDividersEnabled(true);
listView.setAdapter(adapter);
popLogin = new PopupWindow(listView, et_zh.getWidth(), LayoutParams.WRAP_CONTENT);
popLogin.setOutsideTouchable(true);
adapter.setPop(popLogin);
popLogin.showAsDropDown(et_zh);
} else {
adapter.notifyDataSetChanged();
popLogin = new PopupWindow(listView, et_zh.getWidth(), LayoutParams.WRAP_CONTENT);
popLogin.setOutsideTouchable(true);
adapter.setPop(popLogin);
popLogin.showAsDropDown(et_zh);
}
} else {
popLogin.dismiss();
popLogin = null;
}
}
});
}
// 适配器
private class QAdapter extends BaseAdapter {
private final List users;
private PopupWindow pop;
private final LayoutInflater mInflater;
public QAdapter(Activity self, List users, EditText et_zh) {
mInflater = LayoutInflater.from(self);
this.users = users;
}
public int getCount() {
return users.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.f_widget_popupwindow, null);
holder = new Holder();
holder.view = (TextView) convertView.findViewById(R.id.tv_zh);
holder.button = (ImageButton) convertView.findViewById(R.id.ib_sc);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
if (holder != null) {
convertView.setId(position);
holder.setId(position);
holder.view.setText(users.get(position));
holder.view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (pop != null) {
if (pop.isShowing()) {
pop.dismiss();
pop = null;
String useZH = users.get(position);
if (useZH != null) {
et_zh.setText(useZH);
}
}
}
return true;
}
});
holder.button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
users.remove(users.get(position));
QAdapter.this.notifyDataSetChanged();
}
});
}
return convertView;
}
public void setPop(PopupWindow pop) {
this.pop = pop;
}
class Holder {
TextView view;
ImageButton button;
void setId(int position) {
view.setId(position);
button.setId(position);
}
}
}
}