天天看點

android學習之popupWindow

 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();

   }

  });

 }

繼續閱讀