R.id.tabcontent报错的一些问题

对项目做一些简单的框架搭建仍然是用的以前自己写的布局框架。在使用AndroidStudio的时候发现
android.support.v4.app.FragmentTabHost这个方法除了一些问题。如下:

    <?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:orientation="vertical" >  

        <FrameLayout  
            android:id="@+id/realtabcontent"  
            android:layout_width="match_parent"  
            android:layout_height="0dip"  
            android:layout_weight="1" />  

        <android.support.v4.app.FragmentTabHost  
            android:id="@android:id/tabhost"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:background="@drawable/bottom_bg" >  

            <TabWidget  
                android:id="@android:id/tabs"  
                android:layout_width="match_parent"  
                android:layout_height="wrap_content"  
                android:background="#000000"  
                android:orientation="horizontal" />  
        </android.support.v4.app.FragmentTabHost>  

    </LinearLayout>  

之后在网上找到了一篇博客,问题解决:http://blog.youkuaiyun.com/deng0zhaotai/article/details/48295285

这边文章真心难找,在百度的搜索方式不一样,一般是搜索R.id.tabcontent的错误日志,这里是
“fragmenttabhost在新版v4包中”才能搜到这篇文章,我一开始也没想到是新V4包的版本问题,
因为androidstudio是最新下载的,创建项目的时候V4包自动使用最新的,所以我以前使用的V4
包是没这个bug的,而新版中暴露了出来,尝试使用了最新的android.support.design.widget.TabLayout,发现有Bug,当标签栏在底部时,系统会先加载内容页,
而不加载标签页,网上说开一个线程来手动加载标签页,我试了一下还是不行。。。如果有人成功了
麻烦告诉我方法,非常感谢!
这里对上述引用的博客再做一些补充。
博客所说的最后一个代码片段描述如下:

    <?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:orientation="vertical" >  

        <FrameLayout  
            android:id="@+id/realtabcontent"  
            android:layout_width="match_parent"  
            android:layout_height="0dip"  
            android:layout_weight="1" />  

        <android.support.v4.app.FragmentTabHost  
            android:id="@android:id/tabhost"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:background="@drawable/bottom_bg" >  
        </android.support.v4.app.FragmentTabHost>  

    </LinearLayout>  

遮挡的问题,在

<android.support.v4.app.FragmentTabHost ><<android.support.v4.app.FragmentTabHost />

这段代码中少了TabWidget这个控件,添加进去之后,按照其所说自定义了framelayout的id
代码中进行相应的更改,可仍然是不行的,提示“R.id.content XXXXXX”错误,我这里直接用的
倒数第二个代码段:

    <?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:orientation="vertical" >  

        <android.support.v4.app.FragmentTabHost  
            android:id="@android:id/tabhost"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content" >  

            <FrameLayout  
                android:id="@android:id/tabcontent"  
                android:layout_width="match_parent"  
                android:layout_height="match_parent" />  

            <TabWidget  
                android:id="@android:id/tabs"  
                android:layout_width="match_parent"  
                android:layout_height="wrap_content"  
                android:layout_alignParentBottom="true"  
                android:layout_gravity="bottom"  
                android:background="#B0C4DE"  
                android:orientation="horizontal"  
                android:showDividers="middle" />  
        </android.support.v4.app.FragmentTabHost>  

    </LinearLayout>  

关于这个布局会导致覆盖掉TabWidget控件的问题,我这里在TabWidget所包含既一个标签布局中,
使用了warpcontent,然后将TabWidget控件修改:android:layout_height=”40dp”。一般底部标签
不会有多大,所以这里直接定义了一个比较高的dp值,一般是没什么问题的。如果有完美解决覆盖
问题的请告诉我,谢谢~

现在是在datepicker_layout.xml代码完全一致的前提下,在代码WeatherInfoActivity和FanHui代码中使用的datepicker_layout时间选择器显示的布局样式还是完全不一样,给我直接一次性解决布局样式完全不一样的问题。WeatherInfoActivity:package com.jd.projects.wlw.weatherinfo; import static com.jd.projects.wlw.weatherinfo.AllInfoMap.KEY_SITE_INFO; import android.annotation.SuppressLint; import android.app.AlertDialog; import android.app.DatePickerDialog; import android.app.Dialog; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.util.Log; import android.view.Display; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.DatePicker; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.Spinner; import android.widget.TextView; import android.widget.TimePicker; import com.jd.projects.wlw.DeviceMapActivity; import com.jd.projects.wlw.R; import com.jd.projects.wlw.bean.MapDataBean; import com.jd.projects.wlw.bean.new_webservice.MapDataBeanNew; import com.jd.projects.wlw.fragment.CureDataFragment; import com.jd.projects.wlw.fragment.HistoryDataFragment; import com.jd.projects.wlw.fragment.MonthDataFragment; import com.jd.projects.wlw.fragment.RealTimeFragment; import com.jd.projects.wlw.fragment.TjfxDataFragment; import com.jd.projects.wlw.update.Utils; import java.text.SimpleDateFormat; import java.util.Calendar; public class WeatherInfoActivity extends FragmentActivity implements OnClickListener { private LinearLayout layout_tqyb, layout_nyqx, layout_nyzx, layout_gdnq, layout_tjfx,layout_location; private ImageView image_ntqx, image_tqyb, image_nyzx, image_gdnq, image_tjfx; private Fragment mContent; public static MapDataBean realdata; // public static String asitename; private Calendar calendar = Calendar.getInstance(); private Context context; private ArrayAdapter<String> spinneradapter; private static final String[] m2 = {"空气温度", "空气湿度", "土壤温度1", "土壤温度2", "土壤温度3", "土壤湿度", "光照度", "蒸发量", "降雨量", "风速", "风向", "结露", "气压", "总辐射", "光合有效辐射"}; private static final String[] m1 = {"空气温度", "空气湿度", "土壤温度1", "土壤温度2", "土壤温度3", "土壤湿度1", "土壤湿度2", "土壤湿度3"}; private String spinnervaluse02; TextView time1; private String nsitetype; private MapDataBeanNew curSiteInfo; private double intentLat = 0.0; private double intentLng = 0.0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_wheatherinfo); try { intentLat = getIntent().getDoubleExtra("intentLat", 0.0); intentLng = getIntent().getDoubleExtra("intentLng", 0.0); context = this; //getData(); curSiteInfo = (MapDataBeanNew) getIntent().getSerializableExtra(KEY_SITE_INFO); initView(); if(intentLat == 0 || intentLng == 0){ layout_location.setVisibility(View.GONE); }else{ layout_location.setVisibility(View.VISIBLE); } } catch (Exception e){ Log.d("mcg",e.getMessage()); e.printStackTrace(); } } private void getData() { SharedPreferences preferences = getSharedPreferences("wlw_settings", MODE_PRIVATE); String neiip = preferences.getString("neiip", ""); String mark = preferences.getString("netmode", "");//内网访问还是外网访问? nsitetype = AllInfoMap.nsitetype;// } private void initView() { nsitetype = AllInfoMap.nsitetype;// layout_tqyb = (LinearLayout) findViewById(R.id.tab1); layout_nyqx = (LinearLayout) findViewById(R.id.tab2); layout_nyzx = (LinearLayout) findViewById(R.id.tab3); layout_gdnq = (LinearLayout) findViewById(R.id.tab4); layout_tjfx = (LinearLayout) findViewById(R.id.tab5); layout_location = (LinearLayout) findViewById(R.id.tab6); image_ntqx = (ImageView) findViewById(R.id.image_qixiang);//2 image_tqyb = (ImageView) findViewById(R.id.image_yubao);//1 image_nyzx = (ImageView) findViewById(R.id.image_zixun);//3 image_gdnq = (ImageView) findViewById(R.id.image_gdnq);//4 image_tjfx = (ImageView) findViewById(R.id.image_tjfx);//5 layout_tqyb.setOnClickListener(this); layout_nyqx.setOnClickListener(this); layout_nyzx.setOnClickListener(this); layout_gdnq.setOnClickListener(this); layout_tjfx.setOnClickListener(this); layout_location.setOnClickListener(this); mContent = RealTimeFragment.newInstance(curSiteInfo); getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, mContent).commit(); } @SuppressLint("NonConstantResourceId") @Override public void onClick(View v) { switch (v.getId()) { case R.id.tab1: layout_tqyb.setBackgroundResource(R.drawable.tabshape_bg); layout_nyqx.setBackgroundResource(R.color.transparent); layout_nyzx.setBackgroundResource(R.color.transparent); layout_gdnq.setBackgroundResource(R.color.transparent); layout_tjfx.setBackgroundResource(R.color.transparent); image_tjfx.setImageResource(R.drawable.sh_wxry_rwcx_02); image_nyzx.setImageResource(R.drawable.curve); image_ntqx.setImageResource(R.drawable.history_pic); image_gdnq.setImageResource(R.drawable.disease_pic); image_tqyb.setImageResource(R.drawable.real_pic_on); mContent = RealTimeFragment.newInstance(curSiteInfo); getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, mContent).commit(); break; case R.id.tab2: layout_tqyb.setBackgroundResource(R.color.transparent); layout_nyqx.setBackgroundResource(R.drawable.tabshape_bg); layout_nyzx.setBackgroundResource(R.color.transparent); layout_gdnq.setBackgroundResource(R.color.transparent); layout_tjfx.setBackgroundResource(R.color.transparent); image_tjfx.setImageResource(R.drawable.sh_wxry_rwcx_02); image_gdnq.setImageResource(R.drawable.disease_pic); image_tqyb.setImageResource(R.drawable.real_pic); image_ntqx.setImageResource(R.drawable.history_pic_on); image_nyzx.setImageResource(R.drawable.curve); mContent = HistoryDataFragment.newInstance(curSiteInfo); getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, mContent).commit(); break; case R.id.tab3: showExitGameAlert(); break; case R.id.tab4: layout_tqyb.setBackgroundResource(R.color.transparent); layout_nyqx.setBackgroundResource(R.color.transparent); layout_nyzx.setBackgroundResource(R.color.transparent); layout_gdnq.setBackgroundResource(R.drawable.tabshape_bg); layout_tjfx.setBackgroundResource(R.color.transparent); image_tjfx.setImageResource(R.drawable.sh_wxry_rwcx_02); image_gdnq.setImageResource(R.drawable.disease_pic_on); image_tqyb.setImageResource(R.drawable.real_pic); image_ntqx.setImageResource(R.drawable.history_pic); image_nyzx.setImageResource(R.drawable.curve); mContent = CureDataFragment.newInstance(curSiteInfo); getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, mContent).commit(); break; case R.id.tab5: jinDuJiaozhang(); break; case R.id.tab6: Intent intent = new Intent(WeatherInfoActivity.this, DeviceMapActivity.class); intent.putExtra("intentLat",intentLat); intent.putExtra("intentLng",intentLng); startActivity(intent); break; case R.id.dateselect1: //弹窗日期选择 new MonPickerDialog(context, dateListener1, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show(); break; } } private DatePickerDialog.OnDateSetListener dateListener1 = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) { calendar.set(Calendar.YEAR, arg1);// 将给定的日历字段设置为给定值。 //calendar.set(Calendar.MONTH, arg2); //calendar.set(Calendar.DAY_OF_MONTH, arg3); SimpleDateFormat df = new SimpleDateFormat("yyyy"); time1.setText(df.format(calendar.getTime())); } }; /** * 时间选择器 */ @SuppressLint("SimpleDateFormat") private void showExitGameAlert() { final AlertDialog dlg = new AlertDialog.Builder(this).create(); dlg.show(); Window window = dlg.getWindow(); // *** 主要就是在这里实现这种效果的. // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容 window.setContentView(R.layout.datepicker_layout); // 为确认按钮添加事件,执行退出应用操作 DatePicker dp = (DatePicker) window.findViewById(R.id.dpPicker); final Calendar calendar = Calendar.getInstance(); // final SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月"); final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); // 隐藏日期View ((ViewGroup) ((ViewGroup) dp.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE); dp.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), (view, year, monthOfYear, dayOfMonth) -> { // 获取一个日历对象,并初始化为当前选中的时间 calendar.set(year, monthOfYear, dayOfMonth); }); RelativeLayout ok = (RelativeLayout) window.findViewById(R.id.YES); ok.setOnClickListener(v -> { layout_tqyb.setBackgroundResource(R.color.transparent); layout_nyqx.setBackgroundResource(R.color.transparent); layout_nyzx.setBackgroundResource(R.drawable.tabshape_bg); layout_gdnq.setBackgroundResource(R.color.transparent); layout_tjfx.setBackgroundResource(R.color.transparent); image_tjfx.setImageResource(R.drawable.sh_wxry_rwcx_02); image_gdnq.setImageResource(R.drawable.disease_pic); image_tqyb.setImageResource(R.drawable.real_pic); image_ntqx.setImageResource(R.drawable.history_pic); image_nyzx.setImageResource(R.drawable.curve_hover); String dataTime = format.format(calendar.getTime()); // 携带数据跳转页面 mContent = new MonthDataFragment(); Bundle bundle = new Bundle(); bundle.putString("datatime", dataTime); bundle.putSerializable(KEY_SITE_INFO, curSiteInfo); mContent.setArguments(bundle); getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, mContent).commit(); dlg.cancel(); }); // 关闭alert对话框架 RelativeLayout cancel = (RelativeLayout) window.findViewById(R.id.NO); cancel.setOnClickListener(v -> dlg.cancel()); } /** * 重写datePicker 1.只显示 年-月 2.title 只显示 年-月 * * @author lmw */ public class MonPickerDialog extends DatePickerDialog { public MonPickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) { super(context, callBack, year, monthOfYear, dayOfMonth); //this.setTitle(year + "年" + (monthOfYear + 1) + "月"); this.setTitle(year + "年"); ((ViewGroup) ((ViewGroup) this.getDatePicker().getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE); ((ViewGroup) ((ViewGroup) this.getDatePicker().getChildAt(0)).getChildAt(0)).getChildAt(1).setVisibility(View.GONE); } @Override public void onDateChanged(DatePicker view, int year, int month, int day) { super.onDateChanged(view, year, month, day); //this.setTitle(year + "年" + (month + 1) + "月"); this.setTitle(year + "年"); } } private void jinDuJiaozhang() { final Dialog myDialog = new Dialog(context); //dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0)); myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); myDialog.show(); // 设置宽度为屏幕的宽度 WindowManager windowManager = getWindowManager(); Display display = windowManager.getDefaultDisplay(); WindowManager.LayoutParams lp = myDialog.getWindow().getAttributes(); lp.width = (int) (display.getWidth()); // 设置宽度 myDialog.getWindow().setAttributes(lp); //myDialog.setCancelable(false);//调用这个方法时,按对话框以外的地方不起作用。按返回键也不起作用 myDialog.setCanceledOnTouchOutside(false);//调用这个方法时,按对话框以外的地方不起作用。按返回键还起作用 Window window = myDialog.getWindow(); window.setContentView(R.layout.dialog_et22);// setContentView()必须放在show()的后面,不然会报错 Spinner sp_01 = (Spinner) window.findViewById(R.id.sp_01); // 将可选内容与ArrayAdapter连接起来 if (nsitetype.equals("01") || !Utils.isOldDevice(curSiteInfo.getId())) { // 十五项因子 spinneradapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, m2); } else if (nsitetype.equals("02")) { // 八项因子 spinneradapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, m1); } // 设置下拉列表的风格 spinneradapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // 将adapter 添加到spinner中 sp_01.setAdapter(spinneradapter); sp_01.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub /*if (position == 0) { spinnervaluse02 = "卵"; } else if (position == 1) { spinnervaluse02 = "幼虫"; } else if (position == 2) { spinnervaluse02 = "蛹"; } else if (position == 3) { spinnervaluse02 = "成虫"; }*/ if (nsitetype.equals("01") || !Utils.isOldDevice(curSiteInfo.getId())) { // 十五项因子 spinnervaluse02 = m2[position]; } else if (nsitetype.equals("02")) { // 八项因子 spinnervaluse02 = m1[position]; } } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } }); time1 = (TextView) window.findViewById(R.id.time1); LinearLayout dateselect1 = (LinearLayout) window.findViewById(R.id.dateselect1); // 初始化当前时间 updateDate(); dateselect1.setOnClickListener(this); Button btn_ensure = (Button) window.findViewById(R.id.btn_ensure); Button btn_cancel = (Button) window.findViewById(R.id.btn_cancel); btn_ensure.setOnClickListener(v -> { //spinnervaluse02 time1 //统计分析 layout_tqyb.setBackgroundResource(R.color.transparent); layout_nyqx.setBackgroundResource(R.color.transparent); layout_nyzx.setBackgroundResource(R.color.transparent); layout_gdnq.setBackgroundResource(R.color.transparent); layout_tjfx.setBackgroundResource(R.drawable.tabshape_bg); image_tjfx.setImageResource(R.drawable.sh_wxry_rwcx_01); image_gdnq.setImageResource(R.drawable.disease_pic); image_tqyb.setImageResource(R.drawable.real_pic); image_ntqx.setImageResource(R.drawable.history_pic); image_nyzx.setImageResource(R.drawable.curve); mContent =new TjfxDataFragment(); Bundle bundle = new Bundle(); bundle.putString("spinnervaluse", spinnervaluse02); bundle.putString("time1", time1.getText().toString()); bundle.putSerializable(KEY_SITE_INFO,curSiteInfo); mContent.setArguments(bundle); getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, mContent).commit(); myDialog.dismiss(); }); btn_cancel.setOnClickListener(v -> { // TODO Auto-generated method stub myDialog.dismiss(); }); } private void updateDate() {//时间控件 SimpleDateFormat df = new SimpleDateFormat("yyyy"); time1.setText(df.format(calendar.getTime())); } } FanHui:package com.videogo.ui.login; import android.app.AlertDialog; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.DatePicker; import android.widget.ImageButton; import android.widget.RelativeLayout; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import com.videogo.openapi.EZOpenSDK; import com.videogo.widget.TitleBar; import ezviz.ezopensdk.R; import java.util.Calendar; import java.util.Locale; public class FanHui extends AppCompatActivity { private static final String TAG = "EZPreview"; private String mAppKey; private String mDeviceSerial; private String mVerifyCode; private String mAccessToken; private int mCameraNo; private TextView mDateTextView; private int mSelectedYear, mSelectedMonth, mSelectedDay; private static final String KEY_APPKEY = "appkey"; private static final String KEY_SERIAL = "serial"; private static final String KEY_VERIFYCODE = "VerifyCode"; private static final String KEY_ACCESSTOKEN = "accessToken"; private static final String KEY_CAMERANO = "cameraNo"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ez_playback_list_page); extractParametersFromIntent(); final Calendar calendar = Calendar.getInstance(); mSelectedYear = calendar.get(Calendar.YEAR); mSelectedMonth = calendar.get(Calendar.MONTH); mSelectedDay = calendar.get(Calendar.DAY_OF_MONTH); // 设置日期显示模块 setupDatePicker(); View fanHui = findViewById(R.id.fanhui); fanHui.setOnClickListener(v -> finish()); Button huifangBtn = findViewById(R.id.fanhui); huifangBtn.setOnClickListener(v -> { Intent intent = new Intent(FanHui.this, MainActivity.class); intent.putExtra("deviceSerial", mDeviceSerial); intent.putExtra("cameraNo", mCameraNo); intent.putExtra("accessToken", mAccessToken); intent.putExtra("appkey", mAppKey); intent.putExtra("verifyCode", mVerifyCode); startActivity(intent); }); } private void setupDatePicker() { mDateTextView = findViewById(R.id.date_text); ImageButton datePickerButton = findViewById(R.id.date_picker_button); updateDateDisplay(); datePickerButton.setOnClickListener(v -> showDatePickerDialog()); } private void updateDateDisplay() { String formattedDate = String.format(Locale.getDefault(), "%d年%02d月%02d日", mSelectedYear, mSelectedMonth + 1, // 月份需要+1 mSelectedDay); mDateTextView.setText(formattedDate); } private void showDatePickerDialog() { final AlertDialog dlg = new AlertDialog.Builder(this).create(); dlg.show(); Window window = dlg.getWindow(); window.setContentView(R.layout.datepicker_layout); // 设置对话框宽度 WindowManager.LayoutParams lp = window.getAttributes(); lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 匹配父容器宽度 window.setAttributes(lp); // 初始化日期选择器 DatePicker dpPicker = window.findViewById(R.id.dpPicker); dpPicker.init(mSelectedYear, mSelectedMonth, mSelectedDay, null); // 获取按钮 RelativeLayout yesButton = window.findViewById(R.id.YES); RelativeLayout noButton = window.findViewById(R.id.NO); // 设置确定按钮点击事件 yesButton.setOnClickListener(v -> { mSelectedYear = dpPicker.getYear(); mSelectedMonth = dpPicker.getMonth(); mSelectedDay = dpPicker.getDayOfMonth(); updateDateDisplay(); dlg.dismiss(); }); // 设置取消按钮点击事件 noButton.setOnClickListener(v -> dlg.dismiss()); } private void extractParametersFromIntent() { Bundle extras = getIntent().getExtras(); if (extras != null) { mAppKey = extras.getString(KEY_APPKEY, ""); mDeviceSerial = extras.getString(KEY_SERIAL, ""); mVerifyCode = extras.getString(KEY_VERIFYCODE, ""); mAccessToken = extras.getString(KEY_ACCESSTOKEN, ""); mCameraNo = extras.getInt(KEY_CAMERANO, 0); Log.d(TAG, "Received parameters:"); Log.d(TAG, "AppKey: " + mAppKey); Log.d(TAG, "DeviceSerial: " + mDeviceSerial); Log.d(TAG, "VerifyCode: " + mVerifyCode); Log.d(TAG, "AccessToken: " + mAccessToken); Log.d(TAG, "CameraNo: " + mCameraNo); } else { Log.e(TAG, "No parameters received from intent"); } } }
06-26
对以下代码进行拆分,自动识别功能项,拆分到多个文件中,避免单个文件过长,不能影响功能使用import tkinter as tk from tkinter import ttk, filedialog, messagebox, scrolledtext #import xml.etree.ElementTree as ET from lxml import etree as ET import os import re from xml.dom import minidom from collections import OrderedDict, defaultdict class ConfigUpdaterApp: def __init__(self, root): self.root = root self.root.title("ID统一配置文件自动更新工具") self.root.geometry("1920x1080") self.root.configure(bg="#f0f0f0") # 创建样式 self.style = ttk.Style() self.style.configure("TButton", padding=6, relief="flat", background="#4a7a8c", foreground="blue") self.style.configure("Treeview", font=("Consolas", 10), rowheight=25) self.style.configure("Treeview.Heading", font=("Arial", 11, "bold")) self.style.configure("TNotebook", background="#f0f0f0") self.style.configure("TNotebook.Tab", padding=(10, 5), font=("Arial", 10, "bold")) self.style.configure("XMLText", font=("Consolas", 10), background="#f8f8f8") # 创建主框架 self.create_widgets() # 初始化数据结构 self.rawdata_template = [] self.rawdata_source = None self.event_template = [] self.event_source = None self.current_filter = "" self.variable_mapping = {} self.module_mapping = {} self.init_module() self.event_categories = [] def init_module(self): """ 初始化moduleID与SVEC模板中一致 :return: """ self.module_mapping["System"] = "00" self.module_mapping["EFEM"] = "01" self.module_mapping["Transfer"] = "02" self.module_mapping["Buffer"] = "03" self.module_mapping["LA"] = "11" self.module_mapping["LB"] = "12" self.module_mapping["LC"] = "13" self.module_mapping["LD"] = "14" self.module_mapping["LAB"] = "15" self.module_mapping["LCD"] = "16" self.module_mapping["Ch1"] = "21" self.module_mapping["Ch2"] = "22" self.module_mapping["Ch3"] = "23" self.module_mapping["Ch4"] = "24" self.module_mapping["Ch5"] = "25" self.module_mapping["Ch6"] = "26" self.module_mapping["ChA"] = "27" self.module_mapping["ChB"] = "28" self.module_mapping["ChC"] = "29" self.module_mapping["ChD"] = "30" self.module_mapping["ChE"] = "31" self.module_mapping["ChF"] = "32" def get_module_id(self, module_name): """ 根据module_name和映射表获取id :param module_name: :return: """ if module_name not in self.module_mapping: return None else: return self.module_mapping[module_name] def create_widgets(self): """创建界面组件""" # 创建选项卡 self.notebook = ttk.Notebook(self.root) self.notebook.pack(fill=tk.BOTH, expand=True, padx=10, pady=10) # RawdataConfig 标签页 self.rawdata_frame = ttk.Frame(self.notebook) self.notebook.add(self.rawdata_frame, text="RawdataConfig") self.create_rawdata_tab(self.rawdata_frame) # EventConfig 标签页 self.event_frame = ttk.Frame(self.notebook) self.notebook.add(self.event_frame, text="EventConfig") self.create_event_tab(self.event_frame) # 状态栏 self.status_var = tk.StringVar(value="就绪") status_bar = ttk.Label(self.root, textvariable=self.status_var, relief=tk.SUNKEN, anchor=tk.W, padding=5) status_bar.pack(side=tk.BOTTOM, fill=tk.X) def create_rawdata_tab(self, parent): """创建RawdataConfig标签页""" # 顶部按钮区域 btn_frame = ttk.Frame(parent) btn_frame.pack(fill=tk.X, padx=10, pady=10) # RawdataConfig 按钮 ttk.Button(btn_frame, text="1.加载模板文件", command=self.load_rawdata_template).pack(side=tk.LEFT, padx=5) ttk.Button(btn_frame, text="2.加载源文件", command=self.load_rawdata_source).pack(side=tk.LEFT, padx=5) ttk.Button(btn_frame, text="3.新增ID属性", command=self.add_id_to_rawdata).pack(side=tk.LEFT, padx=5) ttk.Button(btn_frame, text="4.筛选模板Variable", command=self.filter_template_vars).pack(side=tk.LEFT, padx=5) ttk.Button(btn_frame, text="5.筛选源文件Variable", command=self.filter_source_vars).pack(side=tk.LEFT, padx=5) ttk.Button(btn_frame, text="6.对比", command=self.compare_vars).pack(side=tk.LEFT, padx=5) ttk.Button(btn_frame, text="7.导出标红内容", command=self.export_highlighted).pack(side=tk.LEFT, padx=5) ttk.Button(btn_frame, text="8.填充ID属性", command=self.fill_ids).pack(side=tk.LEFT, padx=5) ttk.Button(btn_frame, text="9.删除空ID节点", command=self.filter_empty_id_nodes).pack(side=tk.LEFT, padx=5) ttk.Button(btn_frame, text="10.导出文件", command=self.export_rawdata).pack(side=tk.LEFT, padx=5) ttk.Button(btn_frame, text="11.拆分文件并导出", command=self.split_export).pack(side=tk.LEFT, padx=5) # 创建分割面板 paned_window = ttk.PanedWindow(parent, orient=tk.VERTICAL) paned_window.pack(fill=tk.BOTH, expand=True, padx=10, pady=(0, 10)) # 上半部分 - 模板显示 template_frame = ttk.LabelFrame(paned_window, text="模板显示") paned_window.add(template_frame, weight=1) # 模板表格 self.create_template_table(template_frame) # 下半部分 - 源文件显示(XML原始格式) source_frame = ttk.LabelFrame(paned_window, text="源文件显示(XML原始格式)") paned_window.add(source_frame, weight=1) # XML文本显示区域 self.rawdata_xml_text = scrolledtext.ScrolledText( source_frame, wrap=tk.WORD, font=("Consolas", 10), bg="#f8f8f8", padx=10, pady=10 ) self.rawdata_xml_text.pack(fill=tk.BOTH, expand=True, padx=5, pady=5) self.rawdata_xml_text.config(state=tk.DISABLED) # 初始为只读 # 底部筛选区域 filter_frame = ttk.Frame(parent) filter_frame.pack(fill=tk.X, padx=10, pady=(0, 10)) # 模板Variable筛选 ttk.Label(filter_frame, text="模板Variable筛选:").pack(side=tk.LEFT, padx=(0, 5)) self.template_filter_var = tk.StringVar() self.template_filter_box = tk.Listbox(filter_frame, listvariable=self.template_filter_var, height=6, width=30, selectmode=tk.EXTENDED) self.template_filter_box.pack(side=tk.LEFT, padx=5, pady=5, fill=tk.BOTH, expand=True) # 源文件Variable筛选 ttk.Label(filter_frame, text="源文件Variable筛选:").pack(side=tk.LEFT, padx=(20, 5)) self.source_filter_var = tk.StringVar() self.source_filter_box = tk.Listbox(filter_frame, listvariable=self.source_filter_var, height=6, width=30, selectmode=tk.EXTENDED) self.source_filter_box.pack(side=tk.LEFT, padx=5, pady=5, fill=tk.BOTH, expand=True) # 对比结果区域 self.compare_result = tk.Text(filter_frame, height=6, width=30, state=tk.DISABLED) self.compare_result.pack(side=tk.LEFT, padx=5, pady=5, fill=tk.BOTH, expand=True) def create_event_tab(self, parent): """创建EventConfig标签页""" # 顶部按钮区域 btn_frame = ttk.Frame(parent) btn_frame.pack(fill=tk.X, padx=10, pady=10) # EventConfig 按钮 ttk.Button(btn_frame, text="1.加载模板文件", command=self.load_event_template).pack(side=tk.LEFT, padx=5) ttk.Button(btn_frame, text="2.加载源文件", command=self.load_event_source).pack(side=tk.LEFT, padx=5) ttk.Button(btn_frame, text="3.新增ID属性", command=self.add_id_to_event).pack(side=tk.LEFT, padx=5) ttk.Button(btn_frame, text="4.填充ID属性", command=self.fill_event_attributes).pack(side=tk.LEFT, padx=5) ttk.Button(btn_frame, text="5.导出文件", command=self.export_event).pack(side=tk.LEFT, padx=5) ttk.Button(btn_frame, text="6.拆分文件并导出", command=self.split_export_event).pack(side=tk.LEFT, padx=5) # 创建分割面板 paned_window = ttk.PanedWindow(parent, orient=tk.VERTICAL) paned_window.pack(fill=tk.BOTH, expand=True, padx=10, pady=(0, 10)) # 上半部分 - 模板显示 template_frame = ttk.LabelFrame(paned_window, text="模板显示") paned_window.add(template_frame, weight=1) # 模板表格 self.create_event_template_table(template_frame) # 下半部分 - 源文件显示(XML原始格式) source_frame = ttk.LabelFrame(paned_window, text="源文件显示(XML原始格式)") paned_window.add(source_frame, weight=1) # XML文本显示区域 self.event_xml_text = scrolledtext.ScrolledText( source_frame, wrap=tk.WORD, font=("Consolas", 10), bg="#f8f8f8", padx=10, pady=10 ) self.event_xml_text.pack(fill=tk.BOTH, expand=True, padx=5, pady=5) self.event_xml_text.config(state=tk.DISABLED) # 初始为只读 def create_template_table(self, parent): """创建Rawdata模板表格""" # 创建滚动条 scroll_y = ttk.Scrollbar(parent, orient=tk.VERTICAL) scroll_x = ttk.Scrollbar(parent, orient=tk.HORIZONTAL) # 创建Treeview表格 columns = ("name", "type", "variable", "id") self.template_table = ttk.Treeview( parent, columns=columns, show="headings", yscrollcommand=scroll_y.set, xscrollcommand=scroll_x.set, selectmode="extended" ) # 配置列标题 self.template_table.heading("name", text="Name", anchor=tk.W) self.template_table.heading("type", text="Type", anchor=tk.W) self.template_table.heading("variable", text="Variable", anchor=tk.W) self.template_table.heading("id", text="ID", anchor=tk.W) # 配置列宽度 self.template_table.column("name", width=150, anchor=tk.W) self.template_table.column("type", width=120, anchor=tk.W) self.template_table.column("variable", width=300, anchor=tk.W) self.template_table.column("id", width=100, anchor=tk.W) # 配置滚动条 scroll_y.config(command=self.template_table.yview) scroll_x.config(command=self.template_table.xview) # 布局 self.template_table.grid(row=0, column=0, sticky="nsew") scroll_y.grid(row=0, column=1, sticky="ns") scroll_x.grid(row=1, column=0, sticky="ew") # 配置网格权重 parent.grid_rowconfigure(0, weight=1) parent.grid_columnconfigure(0, weight=1) def create_event_template_table(self, parent): """创建Event模板表格""" # 创建滚动条 scroll_y = ttk.Scrollbar(parent, orient=tk.VERTICAL) scroll_x = ttk.Scrollbar(parent, orient=tk.HORIZONTAL) # 创建Treeview表格 columns = ("name", "id", "type") self.event_template_table = ttk.Treeview( parent, columns=columns, show="headings", yscrollcommand=scroll_y.set, xscrollcommand=scroll_x.set, selectmode="extended" ) # 配置列标题 self.event_template_table.heading("name", text="Name", anchor=tk.W) self.event_template_table.heading("id", text="ID", anchor=tk.W) self.event_template_table.heading("type", text="Type", anchor=tk.W) # 配置列宽度 self.event_template_table.column("name", width=100, anchor=tk.W) self.event_template_table.column("id", width=100, anchor=tk.W) self.event_template_table.column("type", width=100, anchor=tk.W) # 配置滚动条 scroll_y.config(command=self.event_template_table.yview) scroll_x.config(command=self.event_template_table.xview) # 布局 self.event_template_table.grid(row=0, column=0, sticky="nsew") scroll_y.grid(row=0, column=1, sticky="ns") scroll_x.grid(row=1, column=0, sticky="ew") # 配置网格权重 parent.grid_rowconfigure(0, weight=1) parent.grid_columnconfigure(0, weight=1) def display_xml_content(self, xml_tree, text_widget): """在文本框中显示格式化的XML内容""" try: # 将XML树转换为字符串 xml_str = ET.tostring(xml_tree.getroot(), encoding='utf-8').decode('utf-8') # 使用minidom进行格式化 dom = minidom.parseString(xml_str) pretty_xml = dom.toprettyxml(indent=" ") # 移除多余的换行 pretty_xml = "\n".join([line for line in pretty_xml.split("\n") if line.strip()]) # 更新文本框 text_widget.config(state=tk.NORMAL) text_widget.delete(1.0, tk.END) text_widget.insert(tk.END, pretty_xml) text_widget.config(state=tk.DISABLED) # 高亮空ID节点(可选) self.highlight_empty_id_nodes(text_widget, pretty_xml) except Exception as e: messagebox.showerror("显示错误", f"格式化XML显示失败: {str(e)}") def highlight_empty_id_nodes(self, text_widget, xml_content): """高亮显示id为空的data节点""" if not xml_content: return # 查找所有id属性为空的data节点 pattern = r'(<data\b[^>]*\bid="")' matches = re.finditer(pattern, xml_content) # 设置高亮样式 text_widget.tag_configure("empty_id", background="#ffe6e6") # 应用高亮 for match in matches: start_index = f"1.0 + {match.start()} chars" end_index = f"1.0 + {match.end()} chars" text_widget.tag_add("empty_id", start_index, end_index) # ================ RawdataConfig 功能 ================ def load_rawdata_template(self): """加载Rawdata模板文件(.txt)""" file_path = filedialog.askopenfilename( title="选择Rawdata模板文件", filetypes=[("Text files", "*.txt"), ("All files", "*.*")] ) if not file_path: return try: with open(file_path, 'r', encoding='utf-8') as file: lines = file.readlines() self.rawdata_template = [] # 清空表格 for item in self.template_table.get_children(): self.template_table.delete(item) # 解析模板文件 for line in lines: line = line.strip() if not line: continue # 分割行数据 parts = line.split() if len(parts) < 4: continue # 提取前四列 name = parts[0] var_type = parts[1] variable = parts[2] var_id = parts[3] if len(parts) > 3 else "" self.rawdata_template.append({ "name": name, "type": var_type, "variable": variable, "id": var_id }) # 添加到表格 self.template_table.insert("", tk.END, values=(name, var_type, variable, var_id)) self.status_var.set(f"已加载Rawdata模板: {os.path.basename(file_path)} - {len(self.rawdata_template)} 条记录") except Exception as e: messagebox.showerror("加载错误", f"加载Rawdata模板文件失败: {str(e)}") def load_rawdata_source(self): """加载Rawdata源文件(.xml)""" file_path = filedialog.askopenfilename( title="选择Rawdata源文件", filetypes=[("XML files", "*.xml"), ("All files", "*.*")] ) if not file_path: return try: self.rawdata_tree = ET.parse(file_path) self.rawdata_root = self.rawdata_tree.getroot() self.rawdata_source = self.rawdata_tree # 在文本框中显示XML内容 self.display_xml_content(self.rawdata_tree, self.rawdata_xml_text) self.status_var.set(f"已加载Rawdata源文件: {os.path.basename(file_path)}") except Exception as e: messagebox.showerror("加载错误", f"加载Rawdata源文件失败: {str(e)}") def add_id_to_rawdata(self): """为Rawdata源文件添加ID属性""" if not self.rawdata_source: messagebox.showwarning("无数据", "请先加载Rawdata源文件") return # 添加ID属性 count = 0 for data in self.rawdata_root.findall('.//data'): if 'id' not in data.attrib: # 在属性开头位置添加id属性 new_attrib = OrderedDict() new_attrib['id'] = '' for key, value in data.attrib.items(): new_attrib[key] = value #data.attrib = new_attrib data.attrib.clear() for key, value in new_attrib.items(): data.set(key, value) count += 1 # 更新XML显示 self.display_xml_content(self.rawdata_tree, self.rawdata_xml_text) self.status_var.set(f"已为{count}个data标签添加id属性") def filter_template_vars(self): """筛选模板Variable""" if not self.rawdata_template: messagebox.showwarning("无数据", "请先加载Rawdata模板文件") return # 提取Variable后半部分并去重 var_parts = set() for item in self.rawdata_template: variable = item["variable"] parts = variable.split('_', 1) # 只分割第一个下划线 if len(parts) > 1: var_parts.add(parts[1]) # 更新列表 self.template_filter_var.set(tuple(sorted(var_parts))) self.status_var.set(f"已筛选出 {len(var_parts)} 个模板Variable") def filter_source_vars(self): """筛选源文件Variable""" if not self.rawdata_source: messagebox.showwarning("无数据", "请先加载Rawdata源文件") return # 提取Variable后半部分并去重 var_parts = set() for data in self.rawdata_root.findall('.//data'): variable = data.get('variable', '') if variable: parts = variable.split('_', 1) # 只分割第一个下划线 if len(parts) > 1: var_parts.add(parts[1]) # 更新列表 self.source_filter_var.set(tuple(sorted(var_parts))) self.status_var.set(f"已筛选出 {len(var_parts)} 个源文件Variable") def compare_vars(self): """对比模板和源文件Variable""" template_vars = self.template_filter_box.get(0, tk.END) source_vars = self.source_filter_box.get(0, tk.END) if not template_vars or not source_vars: messagebox.showwarning("无数据", "请先筛选模板和源文件Variable") return # 清空对比结果 self.compare_result.config(state=tk.NORMAL) self.compare_result.delete(1.0, tk.END) # 查找源文件中有但模板中没有的Variable missing_in_template = set(source_vars) - set(template_vars) # 更新源文件列表,标红缺失项 self.source_filter_box.delete(0, tk.END) for var in sorted(source_vars): self.source_filter_box.insert(tk.END, var) if var in missing_in_template: self.source_filter_box.itemconfig(tk.END, fg='red') # 显示对比结果 self.compare_result.insert(tk.END, "对比结果:\n") self.compare_result.insert(tk.END, f"模板Variable数量: {len(template_vars)}\n") self.compare_result.insert(tk.END, f"源文件Variable数量: {len(source_vars)}\n") self.compare_result.insert(tk.END, f"源文件中有但模板中缺失的数量: {len(missing_in_template)}\n\n") if missing_in_template: self.compare_result.insert(tk.END, "缺失的Variable:\n") for var in sorted(missing_in_template): self.compare_result.insert(tk.END, f" - {var}\n") self.compare_result.config(state=tk.DISABLED) self.status_var.set(f"对比完成: 发现 {len(missing_in_template)} 个缺失项") def export_highlighted(self): """导出标红内容""" # 获取标红的项 highlighted_items = [] for i in range(self.source_filter_box.size()): if self.source_filter_box.itemcget(i, "fg") == "red": highlighted_items.append(self.source_filter_box.get(i)) if not highlighted_items: messagebox.showinfo("无数据", "没有标红的项可导出") return # 选择保存位置 file_path = filedialog.asksaveasfilename( title="保存标红内容", defaultextension=".txt", filetypes=[("Text files", "*.txt"), ("All files", "*.*")] ) if not file_path: return # 写入文件 try: with open(file_path, 'w', encoding='utf-8') as file: file.write("标红的Variable项:\n") for item in highlighted_items: file.write(f"{item}\n") self.status_var.set(f"已导出 {len(highlighted_items)} 个标红项到 {os.path.basename(file_path)}") messagebox.showinfo("导出成功", "标红项已成功导出") except Exception as e: messagebox.showerror("导出错误", f"导出文件失败: {str(e)}") def fill_ids(self): """填充ID属性(占位)""" if not self.rawdata_source: messagebox.showwarning("无数据", "请先加载Rawdata源文件") return if not self.rawdata_root: messagebox.showwarning("无数据", "请先加载Rawdata源文件") return if not self.rawdata_template: messagebox.showwarning("无数据", "请先加载模板文件") return # 自动加载映射表,如果不存在就不加载 self.init_variable_mapping() fill_count = 0 not_exist = [] not_exist_all = [] for data_elem in self.rawdata_tree.findall('.//data'): variable = data_elem.get('variable') parts = variable.split('_', 1) if len(parts) < 2: continue module = parts[0] var = parts[1] temp_var, temp_id = self.get_id(module, var) if temp_var and temp_id: data_elem.set('variable', temp_var) data_elem.set('id', temp_id) fill_count += 1 else: if temp_id is None and var not in not_exist: not_exist.append(var) if temp_id is None: not_exist_all.append(variable) # 更新XML显示 self.display_xml_content(self.rawdata_tree, self.rawdata_xml_text) self.status_var.set(f"已为{fill_count}个data标签添加id属性,剩余{len(not_exist)}无对应值。总共剩余{len(not_exist_all)}个未设置") #messagebox.showinfo("功能待实现", "填充ID属性功能将在后续版本实现") def filter_empty_id_nodes(self): """删除id属性为空的data节点并刷新显示""" if not self.rawdata_source: messagebox.showwarning("无数据", "请先加载Rawdata源文件") return # 获取XML根节点 root = self.rawdata_tree.getroot() # 确保获取根节点[^5] # 查找所有id属性为空的data节点 empty_id_nodes = [] for data_node in root.iter('data'): if data_node.get('id', '') == '': empty_id_nodes.append(data_node) if not empty_id_nodes: messagebox.showinfo("无操作", "未找到id为空的data节点") return # 删除这些节点(使用替代方法获取父节点) for node in empty_id_nodes: # 替代 getparent() 的方法 for parent in root.iter(): if node in list(parent): # 检查节点是否是当前父节点的直接子节点 parent.remove(node) break # 刷新XML显示 self.display_xml_content(self.rawdata_tree, self.rawdata_xml_text) # 更新状态 self.status_var.set(f"已删除 {len(empty_id_nodes)} 个id为空的data节点") def export_rawdata(self): """导出Rawdata源文件""" if not self.rawdata_source: messagebox.showwarning("无数据", "没有可导出的数据") return file_path = filedialog.asksaveasfilename( title="保存Rawdata文件", defaultextension=".xml", filetypes=[("XML files", "*.xml"), ("All files", "*.*")] ) if not file_path: return try: # 使用minidom美化输出 ''' xml_str = ET.tostring(self.rawdata_root, encoding='utf-8').decode('utf-8') parsed = minidom.parseString(xml_str) pretty_xml = parsed.toprettyxml(indent=" ") # 写入文件 with open(file_path, 'w', encoding='utf-8') as file: file.write(pretty_xml) ''' self.rawdata_tree.write(file_path, encoding='utf-8', xml_declaration=True) self.status_var.set(f"Rawdata文件已成功导出到: {os.path.basename(file_path)}") messagebox.showinfo("导出成功", "Rawdata配置文件已成功导出") except Exception as e: messagebox.showerror("导出错误", f"导出文件失败: {str(e)}") def init_variable_mapping(self): with open('SV_mapping.txt', 'r') as file: for line in file.readlines(): parts = line.split() if len(parts) == 2: self.variable_mapping[parts[0]] = parts[1] def get_id(self, module_name, var): if var in self.variable_mapping: var = self.variable_mapping[var] temp_var = "CHXXX_" + var module_id = self.get_module_id(module_name) variable = module_name + '_' + var # Ch1_LotID temp_id = self.get_id_by_variable(temp_var) if temp_id and module_id: id = temp_id.replace('XX', str(module_id)) return variable, id else: return None, None def get_id_by_variable(self, variable): for item in self.rawdata_template: if item['variable'] == variable: return item['id'] def split_export(self): if not self.rawdata_source: messagebox.showwarning("无数据", "没有可拆分的数据") return # 创建分组字典,前缀为data节点列表 groups = defaultdict(list) # 遍历所有data节点 for data in self.rawdata_root.findall('.//data'): variable = data.get('variable','') if '_' in variable: prefix = variable.split('_', 1)[0] groups[prefix].append(data) # 为每个分组创建新的xml文件 for prefix, data in groups.items(): # 创建新根节点(复制原始根节点标签和属性) new_root = ET.Element(self.rawdata_root.tag, attrib=self.rawdata_root.attrib) # 复制原始根节点的命名空间 # 复制原始根节点的命名空间 for ns, uri in self.rawdata_root.nsmap.items(): if ns: ET.register_namespace(ns, uri) # 添加原始非 data 节点 for child in self.rawdata_root: if child.tag != 'data': new_root.append(child) # 添加分组中的 data 节点 for node in data: new_root.append(node) # 创建 XML 树并写入文件 new_tree = ET.ElementTree(new_root) filename = f"RawData_{prefix}.xml" new_tree.write(filename, encoding='utf-8', xml_declaration=True) return # ================ EventConfig 功能 ================ def load_event_template(self): """加载Event模板文件(.txt)""" file_path = filedialog.askopenfilename( title="选择Event模板文件", filetypes=[("Text files", "*.txt"), ("All files", "*.*")] ) if not file_path: return try: with open(file_path, 'r', encoding='utf-8') as file: lines = file.readlines() self.event_template = [] # 清空表格 for item in self.event_template_table.get_children(): self.event_template_table.delete(item) # 解析模板文件 for line in lines: line = line.strip() if not line: continue # 分割行数据 parts = line.split() if len(parts) < 2: continue # 提取前两列 name = parts[0] event_id = parts[1] if len(parts) > 1 else "" # 提取type name_parts = name.split('_',1) if len(name_parts) < 2: continue type = name_parts[1] self.event_categories.append(type) self.event_template.append({ "name": name, "id": event_id, "type": type }) # 添加到表格 self.event_template_table.insert("", tk.END, values=(name, event_id, type)) self.event_categories = sorted(self.event_categories, key=len, reverse=True) self.status_var.set(f"已加载Event模板: {os.path.basename(file_path)} - {len(self.event_template)} 条记录") except Exception as e: messagebox.showerror("加载错误", f"加载Event模板文件失败: {str(e)}") def load_event_source(self): """加载Event源文件(.xml)""" file_path = filedialog.askopenfilename( title="选择Event源文件", filetypes=[("XML files", "*.xml"), ("All files", "*.*")] ) if not file_path: return try: self.event_tree = ET.parse(file_path) self.event_root = self.event_tree.getroot() self.event_source = self.event_tree # 在文本框中显示XML内容 self.display_xml_content(self.event_tree, self.event_xml_text) self.status_var.set(f"已加载Event源文件: {os.path.basename(file_path)}") except Exception as e: messagebox.showerror("加载错误", f"加载Event源文件失败: {str(e)}") def add_id_to_event(self): """为Event源文件添加ID属性""" if not self.event_source: messagebox.showwarning("无数据", "请先加载Event源文件") return # 添加ID属性 count = 0 for event in self.event_root.findall('.//event'): if 'id' not in event.attrib: # 在属性开头位置添加id属性 new_attrib = OrderedDict() new_attrib['id'] = '' for key, value in event.attrib.items(): new_attrib[key] = value # data.attrib = new_attrib event.attrib.clear() for key, value in new_attrib.items(): event.set(key, value) count += 1 # 更新XML显示 self.display_xml_content(self.event_tree, self.event_xml_text) self.status_var.set(f"已为{count}个event元素添加ID属性") def get_id_from_event_template(self, event): for item in self.event_template: if item['name'] == event: return item['id'] def get_id_event(self, module, cat): temp_name = "CHXXX_" + cat module_id = self.get_module_id(module) temp_id = self.get_id_from_event_template(temp_name) name = module + '_' + cat if temp_id and module_id: id = temp_id.replace('XX', str(module_id)) return name, id else: return None, None def split_by_type(self, event_name): if not self.event_categories: return for cat in self.event_categories: if event_name.endswith(cat): print('split_by_type:' + cat +'orign:' + event_name) module = event_name[:-len(cat)] return module, cat def fill_event_attributes(self): """填充Event属性(占位)""" # messagebox.showinfo("功能待实现", "填充属性功能将在后续版本实现") # 加载映射表 待实现 if not self.event_source: messagebox.showwarning("无数据", "请先加载EventConfig源文件") return if not self.event_template: messagebox.showwarning("无数据", "请先加载EventConfig模板文件") return fill_count = 0 not_exist = [] not_exist_all = [] for data_elem in self.event_tree.findall('.//event'): name = data_elem.get('name') module, cat = self.split_by_type(name) print('module:'+module+'cat:'+cat) temp_name, temp_id = self.get_id_event(module, cat) #print('temp_name:'+temp_name+' temp_id:'+temp_id) if temp_name and temp_id: data_elem.set('name', temp_name) data_elem.set('id', temp_id) fill_count += 1 else: if temp_id is None and cat not in not_exist: not_exist.append(cat) if temp_id is None: not_exist_all.append(name) self.display_xml_content(self.event_tree, self.event_xml_text) self.status_var.set(f"已为{fill_count}个event补充id属性,剩余{len(not_exist)}个不在模板中,总共剩余{len(not_exist_all)}个") def export_event(self): """导出Event源文件""" if not self.event_source: messagebox.showwarning("无数据", "没有可导出的数据") return file_path = filedialog.asksaveasfilename( title="保存Event文件", defaultextension=".xml", filetypes=[("XML files", "*.xml"), ("All files", "*.*")] ) if not file_path: return try: self.event_tree.write(file_path, encoding='utf-8', xml_declaration=True) self.status_var.set(f"Event文件已成功导出到: {os.path.basename(file_path)}") messagebox.showinfo("导出成功", "Event配置文件已成功导出") except Exception as e: messagebox.showerror("导出错误", f"导出文件失败: {str(e)}") def split_export_event(self): return if __name__ == "__main__": root = tk.Tk() app = ConfigUpdaterApp(root) root.mainloop()
最新发布
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值