package com.zdsoft.popupwindow1130;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button bt_show, bt_login, bt_exit;
private EditText et_user, et_password;
private View myView;
private PopupWindow popupWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initListener();
}
private void initView() {
bt_show = (Button) findViewById(R.id.bt_show);
//將佈局文件轉換爲View
myView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popupwindow_view, null);
//獲取自定義佈局中的控件
et_user = (EditText) myView.findViewById(R.id.et_user);
et_password = (EditText) myView.findViewById(R.id.et_password);
bt_login = (Button) myView.findViewById(R.id.bt_login);
bt_exit = (Button) myView.findViewById(R.id.bt_exit);
}
private void initListener() {
bt_login.setOnClickListener(this);
bt_exit.setOnClickListener(this);
bt_show.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_login:
Toast.makeText(MainActivity.this, et_user.getText().toString(), Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
break;
case R.id.bt_exit:
//關閉彈出窗口
popupWindow.dismiss();
break;
case R.id.bt_show:
//實例化PopupWindow
popupWindow = new PopupWindow(myView, 600, 400);
//設置可以聚焦
popupWindow.setFocusable(true);
//設置背景顏色
popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(MainActivity.this, R.color.colorAccent));
//顯示彈出窗口
popupWindow.showAtLocation(myView, Gravity.CENTER, 0, 0);
break;
default:
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.zdsoft.popupwindow1130.MainActivity">
<Button
android:id="@+id/bt_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登錄按鈕" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="賬號"/>
<EditText
android:id="@+id/et_user"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="請輸入賬號"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:paddingBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密碼"/>
<EditText
android:id="@+id/et_password"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="請輸入密碼"
android:inputType="textPassword"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/bt_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登錄"
android:layout_marginRight="60dp"/>
<Button
android:id="@+id/bt_exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"/>
</LinearLayout>
</LinearLayout>