import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private View inflate;
private PopupWindow pw;
private AlertDialog dialog;
private EditText show_edit,show_edit2,show_edit3,show_edit4;
private String edit,edit2,edit3,edit4;
private CheckBox show_box1,show_box2;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = getLayoutInflater().inflate(R.layout.popuo, null);
inflate = getLayoutInflater().inflate(R.layout.user, null);
show_edit=(EditText)inflate.findViewById(R.id.show_edit);
show_edit2=(EditText)inflate.findViewById(R.id.show_edit2);
show_edit3=(EditText)view.findViewById(R.id.show_edit3);
show_edit4=(EditText)view.findViewById(R.id.show_edit4);
show_box1=(CheckBox)view.findViewById(R.id.show_box1);
show_box2=(CheckBox)view.findViewById(R.id.show_box2);
sp=getSharedPreferences("count", Context.MODE_APPEND+Context.MODE_PRIVATE);
edit3=sp.getString("show_edit3",null);
edit4=sp.getString("show_edit4",null);
if(edit3!=null){
show_edit3.setText(edit3);
}
if(edit4!=null){
show_edit4.setText(edit4);
}
pw=new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
pw.setFocusable(true);
pw.setOutsideTouchable(true);
}
@Override
public void onCreateContextMenu(ContextMenu menu1, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu1, v, menuInfo);
}
public void onClick(View view) {
switch (view.getId()){
case R.id.show_button:
if (pw.isShowing()){
pw.dismiss();
}else {
pw.showAtLocation(view, Gravity.CENTER,0,0);
}
break;
case R.id.button1:
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setView(inflate);
dialog=builder.create();
dialog.show();
break;
case R.id.button2:
if (show_edit3.getText().toString().equals(show_edit.getText().toString())&&show_edit4.getText().toString().equals(show_edit2.getText().toString())){
if (show_box1.isChecked()){
SharedPreferences.Editor editor=sp.edit();
editor.putString("show_edit3",edit);
editor.commit();
edit3=sp.getString("show_edit3",null);
show_edit3.setText(edit3);
}
if (show_box2.isChecked()){
SharedPreferences.Editor editor1=sp.edit();
editor1.putString("show_edit4",edit);
editor1.commit();
edit4=sp.getString("show_edit4",null);
show_edit4.setText(edit4);
}
if (!show_box1.isChecked()){
}
if (!show_box2.isChecked()){
edit4=null;
}
Toast.makeText(this, "密码正确,登录成功", Toast.LENGTH_SHORT).show();
}
break;
case R.id.show_botton3:
edit=show_edit.getText().toString();
edit2=show_edit2.getText().toString();
show_edit3.setText(edit);
show_edit4.setText(edit2);
dialog.dismiss();
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义账号:"
android:textColor="@android:color/black"
android:layout_gravity="center"
android:textSize="15sp"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5">
<EditText
android:id="@+id/show_edit"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_gravity="center"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义密码:"
android:textColor="@android:color/black"
android:layout_gravity="center"
android:textSize="15sp"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5">
<EditText
android:id="@+id/show_edit2"
android:layout_width="match_parent"
android:layout_height="55dp"
android:inputType="textPassword"
android:layout_gravity="center"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="@+id/show_botton3"
android:onClick="onClick"
android:layout_width="150dp"
android:layout_height="65dp"
android:layout_gravity="center"
android:layout_marginLeft="85dp"
android:text="完成"
android:textSize="23sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"></LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.95"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="20sp"
android:textColor="@android:color/black"
android:layout_gravity="center"
android:layout_marginLeft="25dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2">
<EditText
android:id="@+id/show_edit3"
android:layout_width="240dp"
android:layout_height="50dp"
android:layout_gravity="center"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.95"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"
android:textSize="20sp"
android:textColor="@android:color/black"
android:layout_gravity="center"
android:layout_marginLeft="25dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2">
<EditText
android:id="@+id/show_edit4"
android:layout_width="240dp"
android:layout_height="50dp"
android:inputType="textPassword"
android:layout_gravity="center"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.95"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<CheckBox
android:id="@+id/show_box1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住用户名"
android:textSize="20dp"
android:layout_gravity="center"
android:layout_marginLeft="20dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<CheckBox
android:id="@+id/show_box2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"
android:textSize="20dp"
android:layout_gravity="center"
android:layout_marginLeft="25dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.5"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.5">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:layout_width="140dp"
android:layout_height="65dp"
android:layout_gravity="center"
android:layout_marginLeft="30dp"
android:text="注册"
android:onClick="onClick"
android:textSize="20sp"
android:id="@+id/button1" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:layout_width="140dp"
android:layout_height="65dp"
android:layout_gravity="center"
android:layout_marginLeft="30dp"
android:text="确定"
android:onClick="onClick"
android:textSize="20sp"
android:id="@+id/button2" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></LinearLayout>
</LinearLayout>
<?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:id="@+id/activity_main"
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.jj.useractivity.MainActivity">
<Button
android:id="@+id/show_button"
android:text="点击登录"
android:onClick="onClick"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="30dp"/>
</RelativeLayout>