@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
while(!flag){
initWedget();
flag = true;
}
}
//初始化界面控件
private void initWedget(){
//初始化Handler,用来处理消息
handler = new Handler(LoginActivity.this);
//获取下拉框依附的组件宽度
int width = etPhone.getWidth();
pwidth = width;
//设置点击下拉箭头图片事件,点击弹出PopupWindow浮动下拉框
downimage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(flag){
//显示PopupWindow窗口
popupWindwShowing();
}
}
});
//初始化PopupWindow
initPopuWindow();
}
//初始化填充Adapter所用List数据
private void initDatas(){
//查询myQLite中存进的数据
MySQLiteHelper mySQLiteHelper = new MySQLiteHelper(mActivityContext,“user”,null,2);
HelperUserdao helperUserdao = new HelperUserdao(mActivityContext,mySQLiteHelper);
userList = helperUserdao.query();
}
///初始化PopupWindow
private void initPopuWindow(){
initDatas();
//PopupWindow浮动下拉框布局 ,ListView
View loginwindow = (View)this.getLayoutInflater().inflate(R.layout.dialog_loginuser, null);
listView = (ListView) loginwindow.findViewById(R.id.li_longuser_list);
//设置自定义Adapter
optionsAdapter = new OptionsAdapter(this, handler,userList);
listView.setAdapter(optionsAdapter);
selectPopupWindow = new PopupWindow(loginwindow, pwidth, LinearLayout.LayoutParams.WRAP_CONTENT, true);
selectPopupWindow.setOutsideTouchable(true);
//这一句是为了实现弹出PopupWindow后,当点击屏幕其他部分及Back键时PopupWindow会消失,
//没有这一句则效果不能出来,但并不会影响背景
selectPopupWindow.setBackgroundDrawable(new BitmapDrawable());
}
//显示PopupWindow窗口
public void popupWindwShowing() {
//保证有数据才能显示 PopupWindow窗口,没数据的情况下,可能会窗口打开没法触发关闭,导致某些功能失效
if (userList != null && userList.size() != 0){
selectPopupWindow.showAsDropDown(etPhone,0,-3);
}
}
//PopupWindow消失
public void dismiss(){
selectPopupWindow.dismiss();
}
//处理Hander消息
@Override
public boolean handleMessage(Message message) {
Bundle data = message.getData();
switch(message.what){
case 1:
//选中下拉项,下拉框消失
String number = data.getString(“number”);
String password = data.getString(“password”);
etPassword.setText("");
etPhone.setText(number);
etPassword.setText(password);
//隐藏密码
etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
lipassword.setVisibility(View.VISIBLE);
//显示验证码登录选项
txtmim.setVisibility(View.VISIBLE);
//隐藏
limim.setVisibility(View.GONE);
txtpassword.setVisibility(View.GONE);
dismiss();
break;
case 2://移除下拉项数据
//获取数据
String numbers = data.getString(“number”);
int delIndex = data.getInt(“delIndex”);
userList.remove(delIndex);
//删除数据
MySQLiteHelper mySQLiteHelper = new MySQLiteHelper(mActivityContext,“user”,null,2);
HelperUserdao helperUserdao = new HelperUserdao(mActivityContext,mySQLiteHelper);
//判断文本框中的手机号等于的将被删除手机号就清空文本框
if (etPhone.getText().toString().equals(numbers)){
etPhone.setText("");
etPassword.setText("");
}
//执行删除
helperUserdao.delect(numbers);
//刷新下拉列表
optionsAdapter.notifyDataSetChanged();
//数据条数为0的时候关闭PopupWindow窗口,避免输入框点击不开
if (userList.size() == 0){
dismiss();
}
break;
}
return false;
}
效果图