前面一段時間做一個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);
}
});