前面一段时间做一个app,里面有EditText控件,但是输入的信息量较多,所有不是特别方便。因此想实现点击EditText之后,弹一个框,在里面输入要输入的内容,也方便修改,下面是实现的步骤:
我在实现的时候,重写了PopupWindow这个类:
public class PopWindowUtils extends PopupWindow{
private EditText mPopWindowEditText = null;
private Button mButton = null;
private View mView = null;
private LayoutInflater mLayoutInflater = null;
public PopWindowUtils(Activity activity){
mLayoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = mLayoutInflater.inflate(R.layout.popwindow, null);
mPopWindowEditText = (EditText)mView.findViewById(R.id.mPopWindowEditText);
mButton = (Button)mView.findViewById(R.id.mButton);
this.setContentView(mView);
this.setWidth(LayoutParams.FILL_PARENT);
this.setHeight(180);
this.setFocusable(true);
// this.setAnimationStyle(android.R.anim.slide_in_left);
// this.update();
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MainActivity.onTextChanged(mPopWindowEditText.getText().toString());
dismiss();
}
});
}
}
然后再主Activity中设置了一个回调方法:
public static void onTextChanged(String text){
mEditText.setText(text);
}
在主Activity中的EditText点击弹出PopupWindow:
mEditText.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mEditText.setFocusable(false);
mPopupWindow = new PopWindowUtils(MainActivity.this);
mPopupWindow.showAtLocation(MainActivity.this.findViewById(R.id.main), Gravity.TOP | Gravity.LEFT, 0, 0);
}
});