Android开发之弹出popwindow

本文介绍了一个简单的Android应用功能实现:通过PopWindow设置并保存全局IP地址。利用EditText获取用户输入的IP,通过Button触发保存操作,并使用SharedPreferences持久化存储IP信息,确保应用重启后仍能读取上次设置的IP。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求:

在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);
 }
}

效果图:

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值