5.22

所花时间:6小时

代码量:74

博客篇:1

政策查询Android端实现

checkActicity

package com.example.policy;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.policy.ui.dashboard.DashboardFragment;

public class checkActivity extends AppCompatActivity implements View.OnClickListener {

    private Spinner sp_range;
    private String  range="";
    private String text="";
    private String viadata="";
    private String type="";
    private Button bt_check;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case 0:
                    range = (String) msg.obj;
                    if(range.equals("国家")){
                        range="";
                    }
                    else if (range.equals("外省")){
                        range="^河北";
                    }
                    break;
            }
            return true;
        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check);
        getSupportActionBar().hide();
        sp_range = findViewById(R.id.sp_range);
        sp_range.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                range = parent.getItemAtPosition(position).toString();
                Message msg  = new Message();
                msg.what=0;
                msg.obj=range;
                handler.sendMessage(msg);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        bt_check = findViewById(R.id.bt_check);
        bt_check.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent=new Intent(checkActivity.this, DashboardFragment.class);
        Bundle bundle = new Bundle();
        bundle.putString("range",range);
        bundle.putString("type",type);
        bundle.putString("viadata",viadata);
        bundle.putString("text",text);
        intent.putExtras(bundle);
        startActivity(intent);
    }
}

MainActicity

package com.example.policy;

import android.os.Bundle;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.policy.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(binding.navView, navController);
    }

}

policyActicity

package com.example.policy;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class policyActivity extends AppCompatActivity {

    private TextView tv_name;
    private TextView tv_text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_policy);
        getSupportActionBar().hide();
        tv_name = findViewById(R.id.tv_name);
        tv_text = findViewById(R.id.tv_text);
        SharedPreferences sharedPreferences = getSharedPreferences("data",MODE_PRIVATE);
        tv_name.setText(sharedPreferences.getString("name",""));
        tv_text.setText(Html.fromHtml(sharedPreferences.getString("text","")));
    }
}

 

posted @ 2024-05-22 21:07  umiQa  阅读(3)  评论(0编辑  收藏  举报