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