3.22全局参数的保存_补作业来啦~~

这篇博客介绍了两种保存全局参数的方法:一是使用SharedPreference,详细阐述了保存参数的步骤及查看保存路径的方法;二是通过创建自定义Application来保存,包括创建新package、建立Java类并继承Application的基本流程。作者提到了界面设计和滚动视图的尝试,并展示了部分代码。

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

全局参数的保存:

一、使用SharedPreference保存参数

 

我的具体步骤:

 保存之后查看保存路径的方法:

如果你的里面项目很多就会比较难找 我真的要找到吐了

 界面做好之后应该就是下面这个样子:

但是我这个 emmm...手机太短了吧 可能 等有空了找找方法看看能不能想办法做成滚动视图的那样子(曾经试过 但未成功 下次再试试)

setting.java 相关代码:(使用SharedPreference保存参数)

package com.example.smartfactory000;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SettingActivity extends AppCompatActivity {

    private EditText serverAddress;
    private EditText projectLabel;
    private EditText cloudAccountPassword;
    private EditText cloudAccount;
    private EditText cameraAddress;
    private EditText tempSensorId;
    private EditText tempThresholdValves;
    private EditText humiSensorId;
    private EditText humiThresholdValves;
    private EditText lightSensorId;
    private EditText lightThresholdValues;
    private EditText bodySensorId;
    private EditText lightControlId;
    private EditText tongfSensorId;
    private EditText airConditionId;

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

        initView ();


//        Intent intent=getIntent ();
//        String tempValves=intent.getStringExtra ("tempValves");
//
//        TextView textView=(TextView) findViewById (R.id.tv_temp);
//        textView.setText (tempValves);
//       // TextView.setText=(tempValves);

    }

    //按钮点击事件保存
    public void onClickSaved(View view) {
        //保存数据
        SharedPreferences sharedPref = getSharedPreferences ( "params", MODE_PRIVATE );
        SharedPreferences.Editor editor = sharedPref.edit ();
        editor.putString ( "server_address", serverAddress.getText ().toString () );
        editor.putString ( "project_lable", projectLabel.getText ().toString () );
        editor.putString ( "cloud_account", cloudAccount.getText ().toString () );
        editor.putString ( "cloud_account_passward", cloudAccountPassword.getText ().toString () );
        editor.putString ( "camera_address", cameraAddress.getText ().toString () );
        editor.putString ( "temp_sensor_id", tempSensorId.getText ().toString () );
        editor.putString ( "temp_threshold_values", tempThresholdValves.getText ().toString () );
        editor.putString ( "humi_sensor_id", humiSensorId.getText ().toString () );
        editor.putString ( "humi_threshold_values", humiThresholdValves.getText ().toString () );
        editor.putString ( "light_sensor_id", lightSensorId.getText ().toString () );
        editor.putString ( "light_threshold_values", lightThresholdValues.getText ().toString () );
        editor.putString ( "body_sensor_id", bodySensorId.getText ().toString () );
        editor.putString ( "light_control_id", lightControlId.getText ().toString () );
        editor.putString ( "tongt_seneor_id", tongfSensorId.getText ().toString () );
        editor.putString ( "aircondition_id", airConditionId.getText ().toString () );
        editor.commit ();  //确认
       Toast showToast= Toast.makeText ( this,"保存成功",Toast.LENGTH_SHORT);
        showToast.setGravity ( Gravity.CENTER,0,0 );
        showToast.show ();

    }

    private void initView() {
        serverAddress = (EditText) findViewById ( R.id.et_server_address );
        projectLabel = (EditText) findViewById ( R.id.et_cloud_project_label );
        cloudAccount = (EditText) findViewById ( R.id.et_cloud_account );
        cloudAccountPassword = (EditText) findViewById ( R.id.et_cloud_account_password );
        cameraAddress = (EditText) findViewById ( R.id.et_camera_address );
        tempSensorId = (EditText) findViewById ( R.id.et_temp_sensor_values );
        tempThresholdValves = (EditText) findViewById ( R.id.et_temp_threshold_values );
        humiSensorId = (EditText) findViewById ( R.id.et_humi_sensor_id );
        humiThresholdValves = (EditText) findViewById ( R.id.et_humi_threshold_valves );
        lightSensorId = (EditText) findViewById ( R.id.et_light_sensor_id );
        lightThresholdValues = (EditText) findViewById ( R.id.et_light_threshold_values );
        bodySensorId = (EditText) findViewById ( R.id.et_body_sensor_id );
        lightControlId = (EditText) findViewById ( R.id.et_light_control_id );
        tongfSensorId = (EditText) findViewById ( R.id.et_tongf_control_id );
        airConditionId = (EditText) findViewById ( R.id.et_air_control_id );

    }
}

----------------------------------------------------手动分界线---------------------------------------------------

二、使用用户自定义Application来保存全局参数

 

我的具体步骤如下:

1.新建立应该package文件夹 

 2.在此文件夹下面新建立一个Java类

 3.继承Application

大概步骤就是如此。

--------------------------------------------------------------------------------------------------------------------------

保存按钮事件。保存数据。(白底老师的黑底我的)

 

 

 

 

 使用用户自定义Application来保存全局参数的相关代码:

(还有类的代码没有写完 下次放)

package com.example.smartfactory000;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.smartfactory000.tools.SmartfactoryApplication;

public class SettingActivity extends AppCompatActivity {

    private EditText serverAddress;
    private EditText projectLabel;
    private EditText cloudAccountPassword;
    private EditText cloudAccount;
    private EditText cameraAddress;
    private EditText tempSensorId;
    private EditText tempThresholdValves;
    private EditText humiSensorId;
    private EditText humiThresholdValves;
    private EditText lightSensorId;
    private EditText lightThresholdValues;
    private EditText bodySensorId;
    private EditText lightControlId;
    private EditText tongfSensorId;
    private EditText airConditionId;

    private SmartfactoryApplication smartFactory;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_setting );
        //通过getApplication()方法来得到smartFactory的对象
        smartFactory=(SmartfactoryApplication) getApplication ();

        initView ();


//        Intent intent=getIntent ();
//        String tempValves=intent.getStringExtra ("tempValves");
//
//        TextView textView=(TextView) findViewById (R.id.tv_temp);
//        textView.setText (tempValves);
//       // TextView.setText=(tempValves);

    }

    //按钮点击事件保存
    //保存数据
    public void onClickSaved(View view) {
        smartFactory.setServerAddress (serverAddress.getText ().toString ().trim ());;
        smartFactory.setProjectLabel (projectLabel.getText ().toString ().trim ());;
        smartFactory.setCloudAccount (cloudAccount.getText ().toString ().trim ());
        smartFactory.setCloudAccountPassword (cloudAccountPassword.getText ().toString ().trim ());
        smartFactory.setCameraAddress (cameraAddress.getText ().toString ().trim ());
        smartFactory.setTempSensorId (Integer.parseInt (tempSensorId.getText ().toString ().trim ()));
       // smartFactory.setTempSensorId (tempSensorId.getText ().toString ().trim ());
        smartFactory.setTempThresholdValues (Float.parseFloat (tempThresholdValves.getText ().toString ().trim ()));
        smartFactory.setHumiSensorId (Integer.parseInt (humiSensorId.getText ().toString ().trim ()));
        smartFactory.setHumiThresholdValues (Float.parseFloat (humiThresholdValves.getText ().toString ().trim ()));
        smartFactory.setLightSensorId (Integer.parseInt (lightSensorId.getText ().toString ().trim ()));
        smartFactory.setLightThresholdValues (Float.parseFloat (lightThresholdValues.getText ().toString ().trim ()));
        smartFactory.setBodySensorId (Integer.parseInt (bodySensorId.getText ().toString ().trim ()));
        smartFactory.setLightControlId (Integer.parseInt ( lightControlId.getText ().toString ().trim ()));
        smartFactory.setTongfConditionId (Integer.parseInt (tongfSensorId.getText ().toString ().trim ()));
        smartFactory.setAirConditionId (Integer.parseInt (airConditionId.getText ().toString ().trim ()));

        Toast showToast= Toast.makeText ( this,"保存成功",Toast.LENGTH_SHORT);
        showToast.setGravity ( Gravity.CENTER,0,0 );
        showToast.show ();
//        //保存数据
//        SharedPreferences sharedPref = getSharedPreferences ( "params", MODE_PRIVATE );
//        SharedPreferences.Editor editor = sharedPref.edit ();
//        editor.putString ( "server_address", serverAddress.getText ().toString () );
//        editor.putString ( "project_lable", projectLabel.getText ().toString () );
//        editor.putString ( "cloud_account", cloudAccount.getText ().toString () );
//        editor.putString ( "cloud_account_passward", cloudAccountPassword.getText ().toString () );
//        editor.putString ( "camera_address", cameraAddress.getText ().toString () );
//        editor.putString ( "temp_sensor_id", tempSensorId.getText ().toString () );
//        editor.putString ( "temp_threshold_values", tempThresholdValves.getText ().toString () );
//        editor.putString ( "humi_sensor_id", humiSensorId.getText ().toString () );
//        editor.putString ( "humi_threshold_values", humiThresholdValves.getText ().toString () );
//        editor.putString ( "light_sensor_id", lightSensorId.getText ().toString () );
//        editor.putString ( "light_threshold_values", lightThresholdValues.getText ().toString () );
//        editor.putString ( "body_sensor_id", bodySensorId.getText ().toString () );
//        editor.putString ( "light_control_id", lightControlId.getText ().toString () );
//        editor.putString ( "tongt_seneor_id", tongfSensorId.getText ().toString () );
//        editor.putString ( "aircondition_id", airConditionId.getText ().toString () );
//        editor.commit ();  //确认

    }

    private boolean CheckInput(SmartfactoryApplication smartFactory){
        boolean reslt=true;


        return reslt;
    }


    private void initView() {
        serverAddress = (EditText) findViewById ( R.id.et_server_address );
        projectLabel = (EditText) findViewById ( R.id.et_cloud_project_label );
        cloudAccount = (EditText) findViewById ( R.id.et_cloud_account );
        cloudAccountPassword = (EditText) findViewById ( R.id.et_cloud_account_password );
        cameraAddress = (EditText) findViewById ( R.id.et_camera_address );
        tempSensorId = (EditText) findViewById ( R.id.et_temp_sensor_values );
        tempThresholdValves = (EditText) findViewById ( R.id.et_temp_threshold_values );
        humiSensorId = (EditText) findViewById ( R.id.et_humi_sensor_id );
        humiThresholdValves = (EditText) findViewById ( R.id.et_humi_threshold_valves );
        lightSensorId = (EditText) findViewById ( R.id.et_light_sensor_id );
        lightThresholdValues = (EditText) findViewById ( R.id.et_light_threshold_values );
        bodySensorId = (EditText) findViewById ( R.id.et_body_sensor_id );
        lightControlId = (EditText) findViewById ( R.id.et_light_control_id );
        tongfSensorId = (EditText) findViewById ( R.id.et_tongf_control_id );
        airConditionId = (EditText) findViewById ( R.id.et_air_control_id );

    }
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值