Popupwindow的入门

来源:互联网 发布:蛮荒之怒2进阶数据 编辑:程序博客网 时间:2024/05/16 08:03
<span style="font-size:18px;"> PopupWindow这个类用来实现一个弹出框,PopupWindow是可以指定显示位置的,随便哪个位置都可以,更加灵活</span>
<span style="font-size:18px;"></span> 
<span style="font-size:18px;">import android.app.Activity;import android.content.Intent;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.graphics.Color;import android.graphics.drawable.ColorDrawable;import android.os.Bundle;import android.text.TextUtils;import android.view.Gravity;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup.LayoutParams;import android.widget.Button;import android.widget.EditText;import android.widget.PopupWindow;import android.widget.RelativeLayout;import android.widget.Toast;public class MainActivity extends Activity {private RelativeLayout relativeLayout;private SharedPreferences sharedPreferences;private PopupWindow popupWindow;private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);relativeLayout = (RelativeLayout) findViewById(R.id.rl);button = (Button) findViewById(R.id.button);// 获取spsharedPreferences = getSharedPreferences("config", MODE_PRIVATE);}//登录public void login(View v) {// 实例化登陆的popupWindowView contentView = View.inflate(this, R.layout.login_item, null);Button bt_login = (Button) contentView.findViewById(R.id.bt_login);final EditText et_username = (EditText) contentView.findViewById(R.id.et_username);final EditText et_password = (EditText) contentView.findViewById(R.id.et_password);bt_login.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String username = et_username.getText().toString().trim();String password = et_password.getText().toString().trim();String name = sharedPreferences.getString("name", null);String psw = sharedPreferences.getString("psw", null);if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {//Toast.makeText(MainActivity.this, "用户名或者密码不能为空", 0).show();return;}if (username.equals(name) && password.equals(psw)) {// 跳转Intent intent = new Intent(MainActivity.this,OtherActivity.class);intent.putExtra("name", username);intent.putExtra("psw", password);startActivity(intent);finish();} else {Toast.makeText(MainActivity.this, "用户名或密码错误", 0).show();}}});PopupWindow popupWindow = new PopupWindow(contentView,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);popupWindow.setFocusable(true);popupWindow.setOutsideTouchable(true);popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));// 在某一个控件的左下方// 锚 anchor 在某一个控件下方 x ypopupWindow.showAsDropDown(button, 0, 0);}//快速注册public void regist(View v) {// contentView 弹框展示的布局// width 弹框的宽度View contentView = View.inflate(this, R.layout.regist_item, null);final EditText et_username = (EditText) contentView.findViewById(R.id.et_username);final EditText et_password = (EditText) contentView.findViewById(R.id.et_password);Button bt_regist = (Button) contentView.findViewById(R.id.bt_regist);// 点击popupwidonw中的注册按钮,将信息存入的sp文件中bt_regist.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String username = et_username.getText().toString().trim();String password = et_password.getText().toString().trim();// 存储到文件中sharEditor edit = sharedPreferences.edit();edit.putString("name", username);edit.putString("psw", password);edit.commit();Toast.makeText(MainActivity.this, "存入", 0).show();// 让popupWidonw消失popupWindow.dismiss();}});// 两种 wrap_content 匹配父窗体popupWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);// 设置popupWindow的背景,一般设置为一个透明的颜色背景popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));// 获取焦点popupWindow.setFocusable(true);// 设置在外部可以进行点击popupWindow.setOutsideTouchable(true);// 展示出来// 在具体某一个位置进行显示 ,以一个父控件作为参考// parent 作为参考的父控件// gravity 比重 Gravity.TOP|Gravity.RIGHT 右上角// x ,ypopupWindow.showAtLocation(relativeLayout, Gravity.TOP | Gravity.RIGHT,0, 0);}}</span>
<span style="font-size:18px;"></span> 
<span style="font-size:18px;"></span> 
<span style="font-size:18px;"></span> 

0 0
原创粉丝点击