private void initPopWindow() {
// 加载popupWindow的布局文件
View contentView = LayoutInflater.from(getApplicationContext())
.inflate(R.layout.popup, null);
// 设置popupWindow的背景颜色
contentView.setBackgroundColor(Color.RED);
// 声明一个弹出框
final PopupWindow popupWindow = new PopupWindow(
findViewById(R.id.mainlayout), 200, 300);
// 为弹出框设定自定义的布局
popupWindow.setContentView(contentView);
final EditText editText = (EditText) contentView.findViewById(R.id.editText1);
// 设定当你点击editText时,弹出的输入框是啥样子的。这里设置默认为数字输入哦,这时候你会发现你输入非数字的东西是不行的哦
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
popupWindow.setFocusable(true);
popupWindow.showAsDropDown(button);
Button button_sure = (Button) contentView.findViewById(R.id.button1_sure);
button_sure.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
textView.setText("展示信息:"+editText.getText());
}
});
Button button_cancel = (Button) contentView.findViewById(R.id.button2_cancel);
button_cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
}