Android开发之弹出popwindow

需求:

在Android点击设置按钮,弹出popwindow,设置一个IP全局使用,退出重新打开APP,能保留上次传入的IP。

技术:

popWindow

sharedPreference

代码实现:

UI层 popwindow弹框内容,比较简单 一个编辑框 一个保存按钮

<?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"
    android:background="#ffffff">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_margin="10dp"
        android:gravity="center_vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="服务器IP:"
            android:textSize="20dp"/>
        <EditText
            android:id="@+id/pop_ip_edt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <Button
        android:id="@+id/pop_save_ip_btn"
        android:layout_width="80dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:text="保存"/>

</LinearLayout>

代码层:

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity11";

    SharedPreferences sharedPreferences;

    PopupWindow popupWindow;
    //popWindow中的属性
    Button saveBtn;
    EditText ipEdt;
    String popIpStr;

    ImageView setImg;//设置按钮

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sharedPreferences=getSharedPreferences("Scan",0);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        initView();
        initData();
        showPopSettingIp();
        Log.d(TAG, "onCreate: "+popIpStr);
    }

 private void initData() {

    setImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopSettingIp();
                popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
            }
        });
        
    }

  private void showPopSettingIp() {
        View inflate = getLayoutInflater().inflate(R.layout.pop_ip,null,false);
        saveBtn=inflate.findViewById(R.id.pop_save_ip_btn);
        ipEdt=inflate.findViewById(R.id.pop_ip_edt);
        popupWindow=new PopupWindow(inflate,800,300,true);
        popupWindow.setContentView(inflate);
        popupWindow.setAnimationStyle(androidx.appcompat.R.style.Animation_AppCompat_Dialog);
        updateIpInfo();
        savePop();
    }

    private void savePop() {
        saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ipStr = ipEdt.getText().toString();
                popIpStr= ipStr.replaceAll(" ","");//输入的IP去掉空格
                Log.d(TAG, "onClick: "+popIpStr);

                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("ip",popIpStr);
                editor.commit();

                popupWindow.dismiss();
            }
        });
        updateIpInfo();
    }

    private void updateIpInfo() {
        popIpStr=sharedPreferences.getString("ip",popIpStr);
        ipEdt.setText(popIpStr);
    }

 private void initView() {
  setImg=findViewById(R.id.setting_img);
 }
}

效果图: