Popupwindow的入门

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

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		relativeLayout = (RelativeLayout) findViewById(R.id.rl);
		button = (Button) findViewById(R.id.button);
		// 获取sp
		sharedPreferences = getSharedPreferences("config", MODE_PRIVATE);
	}
	//登录
	public void login(View v) {
		// 实例化登陆的popupWindow
		View 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() {

			@Override
			public 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 y
		popupWindow.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() {

			@Override
			public void onClick(View v) {
				String username = et_username.getText().toString().trim();
				String password = et_password.getText().toString().trim();
				// 存储到文件中shar
				Editor 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 ,y
		popupWindow.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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值