字符串批量替换工具,R资源id动态获取

替换相关安卓中,所有R资源id的引用,为动态获取

     

拖动安卓项目src文件夹至当前工具,替换项目R资源包路径为新设置的R文件路径,工具会自动替换相关所有R资源的引用为动态获取。

源码下载:http://git.oschina.net/scimence/CodeReplace

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace CodeReplace
{
    public partial class Form1 : Form
    {
        public Tools T = new Tools();
        public String[] OpendFilesName; // 所有载入的文件名称

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            string filesName = T.dragDrop(e);      //拖入窗体的文件放下
            OpendFilesName = filesName.Split(';'); //分割为所有的文件名

            label3.Text = "载入文件总数:" + OpendFilesName.Length;
        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            T.dragEnter(e);
        }

        /// <summary>
        /// 执行字符串替换逻辑
        /// </summary>
        private void button1_Click(object sender, EventArgs e)
        {
            //String data = "int[] ids = { R.id.music, R.id.about, R.id.more, R.id.help /* , R.id.CDKey */};";
            //String data = "findViewById(R.id.mem_6);";

            //String data = "				btn.setBackgroundResource(R.drawable.shengyinx);";
            //String from = "R.drawable.*", to = "R.drawable(\"*\")";

            //data = replace(data, from, to);
            //MessageBox.Show("替换逻辑完成!\n" + data);


            String data = "";
            foreach (String fileName in OpendFilesName)
            {
                //String data = T.ToString(fileName);
                data = T.ToString(fileName);
                data = replaceLogic(data, textBox1.Text.Trim(), textBox2.Text.Trim());

                //MessageBox.Show("替换逻辑完成!\n" + data);
                T.SaveToNativeFile(data, fileName, true);
            }
            MessageBox.Show("替换逻辑完成!");
        }

        // R.id.*   R.id("*")
        // 字符串替换逻辑,替换data中的所有from为to
        private String replaceLogic(String data, String from, String to)
        {
            String tmp = data;
            if (!from.Contains("*") && !to.Contains("*")) tmp = tmp.Replace(from, to);
            else if (from.Contains("*") && to.Contains("*"))
            {
                tmp = replace(tmp, from, to);
            }
            else MessageBox.Show("此功能待实现!");

            return tmp;
        }
        // R.id.*   R.id("*")
        // 字符串替换逻辑,替换data中的所有from为to
        private String replaceLogic0(String data, String from, String to)
        {
            String tmp = data;
            if (!from.Contains("*") && !to.Contains("*")) tmp = tmp.Replace(from, to);
            else if (from.Contains("*") && to.Contains("*"))
            {
                tmp = replace(tmp, from + ")", to);
                tmp = replace(tmp, from + "}", to);
                tmp = replace(tmp, from + ",", to);
                tmp = replace(tmp, from + ";", to);
            }
            else MessageBox.Show("此功能待实现!");

            return tmp;
        }
        // int[] ids = { R.id.music, R.id.about, R.id.more, R.id.help /* , R.id.CDKey */};
        // R.id.*,   R.id("*")
        // 字符串替换逻辑,替换data中的所有from = "R.id.*,"为to = "R.id("*"),"
        private String replace(String data, String from, String to)
        {
            String tmp = data;

            String[] fStr = from.Split('*');
            String[] tStr = to.Split('*');

            int index1 = 0;
            while (tmp.Substring(index1).Contains(fStr[0]))
            {
                // 前缀索引
                int index1Tmp = tmp.IndexOf(fStr[0], index1);
                if (index1Tmp > index1) index1 = index1Tmp;

                // 后缀索引
                String c = fStr[1];
                int index2 = tmp.IndexOf(c, index1 + fStr[0].Length);
                if (c.Equals(""))
                {
                    // 默认后缀,取较小的分隔符索引为后缀索引
                    String[] sp = { ")", "}", ",", ";", " " };

                    for (int i = 0; i < sp.Length; i++)
                    {
                        int I = tmp.IndexOf(sp[i], index1 + fStr[0].Length);
                        if (index2 == -1 || (index2 > I && I != -1))
                        {
                            index2 = I;
                            c = sp[i];
                        }
                    }
                }
                if (index2 == -1) continue;


                String fromStr = tmp.Substring(index1, index2 - index1).Replace(c, "").TrimEnd();  // 源串
                index1 += fStr[0].Length;
                String arg = tmp.Substring(index1, index2 - index1).Replace(c, "").TrimEnd();      // 获取*对应的字符串

                String toStr = tStr[0] + arg + tStr[1];     // 目标串
                //tmp = tmp.Replace(fromStr, toStr);          // 替换为目标串
                tmp = tmp.Replace(fromStr + c, toStr + c);  // 替换为目标串(仅替换全字匹配的串)
            }

            return tmp;
        }

        // int[] ids = { R.id.music, R.id.about, R.id.more, R.id.help /* , R.id.CDKey */};
        // R.id.*,   R.id("*")
        // 字符串替换逻辑,替换data中的所有from = "R.id.*,"为to = "R.id("*"),"
        private String replace0(String data, String from, String to)
        {
            String tmp = data;

            String[] fStr = from.Split('*');
            String[] tStr = to.Split('*');

            while (tmp.Contains(fStr[0]))
            {
                int index1 = tmp.IndexOf(fStr[0]);                          // 前缀索引
                int index2 = tmp.IndexOf(fStr[1], index1 + fStr[0].Length); // 后缀索引
                int index3 = tmp.IndexOf(' ', index1 + fStr[0].Length);     // 后缀之前有空' '则取其索引
                if (index3 != -1 && index3 < index2) index2 = index3;
                else if (index2 == -1 && index3 != -1) index2 = index3;
                else if (index2 == -1 && index3 == -1) continue;

                String fromStr = tmp.Substring(index1, index2 - index1).Replace(fStr[1], "").TrimEnd();  // 源串
                index1 += fStr[0].Length;
                String arg = tmp.Substring(index1, index2 - index1).Replace(fStr[1], "").TrimEnd();      // 获取*对应的字符串

                String toStr = tStr[0] + arg + tStr[1];                     // 目标串
                tmp = tmp.Replace(fromStr + fStr[1], toStr + fStr[1]);      // 替换为目标串(仅替换全字匹配的串)
            }

            return tmp;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            String data = "";
            foreach (String fileName in OpendFilesName)
            {
                //String data = T.ToString(fileName);
                data = T.ToString(fileName);    // 获取文件数据

                // 自定义替换逻辑
                data = replaceLogic(data, "com.joym.BigSonGoCart.R", "com.shjc.jsbc.view2d.util.R");
                data = replaceLogic(data, "R.id.*", "R.id(\"*\")");
                data = replaceLogic(data, "R.drawable.*", "R.drawable(\"*\")");
                data = replaceLogic(data, "R.layout.*", "R.layout(\"*\")");
                data = replaceLogic(data, "R.raw.*", "R.raw(\"*\")");
                data = replaceLogic(data, "R.string.*", "R.string(\"*\")");
                data = replaceLogic(data, "R.style.*", "R.style(\"*\")");
                data = replaceLogic(data, "R.menu.*", "R.menu(\"*\")");
                data = replaceLogic(data, "R.anim.*", "R.anim(\"*\")");
                data = replaceLogic(data, "R.array.*", "R.array(\"*\")");
                data = replaceLogic(data, "R.attr.*", "R.attr(\"*\")");


                //MessageBox.Show("替换逻辑完成!\n" + data);
                T.SaveToNativeFile(data, fileName, true);
            }
            MessageBox.Show("替换逻辑完成!");
        }

        private String[] tmpData = { "R.id.*", "R.id(\"*\")", "com.joym.BigSonGoCart.R", "com.shjc.jsbc.view2d.util.R" };
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                tmpData[0] = textBox1.Text;
                tmpData[1] = textBox2.Text;
                textBox1.Text = tmpData[2];
                textBox2.Text = tmpData[3];
            }
            else
            {
                tmpData[2] = textBox1.Text;
                tmpData[3] = textBox2.Text;
                textBox1.Text = tmpData[0];
                textBox2.Text = tmpData[1];
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //String R = T.ToString(OpendFilesName[0]);
            // 获取R文件信息
            String tmp = Data.R_java;
            if (textBox1.Text.Contains(".R") && textBox2.Text.Contains(".R"))
            {
                String fromPak = textBox1.Text, toPak = textBox2.Text;
                fromPak = fromPak.Substring(0, fromPak.LastIndexOf(".R"));
                toPak = toPak.Substring(0, toPak.LastIndexOf(".R"));

                tmp = tmp.Replace("com.joym.BigSonGoCart", fromPak);
                tmp = tmp.Replace("com.shjc.jsbc.view2d.util", toPak);
            }


            // 根据所有载入的文件,判定生成R文件保存路径
            string CurDir = getDir();
            if (CurDir.Equals("")) CurDir = System.AppDomain.CurrentDomain.BaseDirectory;
            CurDir += "R.java";

            // 导出R文件
            T.SaveToNativeFile(tmp, CurDir, true);
            T.MessageWithOpen("成功导出R.java", CurDir);
        }

        private string getDir()
        {
            String tmp = textBox2.Text;
            if (tmp.EndsWith(".R"))
            {
                tmp = tmp.Substring(0, tmp.LastIndexOf(".R"));
                tmp = tmp.Replace('.', '\\');
            }
            else return "";

            if (OpendFilesName == null) return "";
            foreach (String fileName in OpendFilesName)
            {
                if (fileName.Contains(tmp))
                {
                    tmp = fileName.Substring(0, fileName.LastIndexOf(tmp)) + tmp + "\\";
                    return tmp;
                }
            }

            return "";
        }

        // 逆向替换逻辑
        private void button4_Click(object sender, EventArgs e)
        {
            String data = "";
            foreach (String fileName in OpendFilesName)
            {
                //String data = T.ToString(fileName);
                data = T.ToString(fileName);    // 获取文件数据

                // 自定义替换逻辑
                data = replaceLogic(data, "com.shjc.jsbc.view2d.util.R", "com.joym.BigSonGoCart.R");
                data = replaceLogic(data, "R.id(\"*\")", "R.id.*");
                data = replaceLogic(data, "R.drawable(\"*\")", "R.drawable.*");
                data = replaceLogic(data, "R.layout(\"*\")", "R.layout.*");
                data = replaceLogic(data, "R.raw(\"*\")", "R.raw.*");
                data = replaceLogic(data, "R.string(\"*\")", "R.string.*");
                data = replaceLogic(data, "R.style(\"*\")", "R.style.*");
                data = replaceLogic(data, "R.menu(\"*\")", "R.menu.*");
                data = replaceLogic(data, "R.anim(\"*\")", "R.anim.*");
                data = replaceLogic(data, "R.array(\"*\")", "R.array.*");
                data = replaceLogic(data, "R.attr(\"*\")", "R.attr.*");


                //MessageBox.Show("替换逻辑完成!\n" + data);
                T.SaveToNativeFile(data, fileName, true);
            }
            MessageBox.Show("替换逻辑完成!");
        }

        private void label3_Click(object sender, EventArgs e)
        {
            //String CurDir = System.AppDomain.CurrentDomain.BaseDirectory;
            //CurDir += "R.java";

             导出R文件
            //T.SaveToNativeFile(Data.tools, CurDir + "Tools.cs", true);
            //T.SaveToNativeFile(Data.form1, CurDir + "Form1.cs", true);
            //T.SaveToNativeFile(Data.form_design, CurDir + "Form1.Designer.cs", true);
            //T.MessageWithOpen("成功导出R.java", CurDir);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CodeReplace
{
    public class Tools
    {

        //=======================================================
        //其他相关功能
        //=======================================================
        /// <summary>
        /// 文件拖进事件处理:
        /// </summary>
        public void dragEnter(DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))    //判断拖来的是否是文件
                e.Effect = DragDropEffects.Link;                //是则将拖动源中的数据连接到控件
            else e.Effect = DragDropEffects.None;
        }
        /// <summary>
        /// 文件放下事件处理:
        /// 放下, 另外需设置对应控件的 AllowDrop = true; 
        /// 获取的文件名形如 "d:\1.txt;d:\2.txt"
        /// </summary>
        public string dragDrop(DragEventArgs e)
        {
            StringBuilder filesName = new StringBuilder("");
            Array file = (System.Array)e.Data.GetData(DataFormats.FileDrop);//将拖来的数据转化为数组存储

            foreach (object I in file)
            {
                string str = I.ToString();

                System.IO.FileInfo info = new System.IO.FileInfo(str);
                //若为目录,则获取目录下所有子文件名
                if ((info.Attributes & System.IO.FileAttributes.Directory) != 0)
                {
                    str = getAllFiles(str);
                    if (!str.Equals("")) filesName.Append((filesName.Length == 0 ? "" : ";") + str);
                }
                //若为文件,则获取文件名
                else if (System.IO.File.Exists(str))
                    filesName.Append((filesName.Length == 0 ? "" : ";") + str);
            }

            return filesName.ToString();
        }

        /// <summary>
        /// 判断path是否为目录
        /// </summary>
        public bool IsDirectory(String path)
        {
            System.IO.FileInfo info = new System.IO.FileInfo(path);
            return (info.Attributes & System.IO.FileAttributes.Directory) != 0;
        }

        /// <summary>
        /// 获取目录path下所有子文件名
        /// </summary>
        public string getAllFiles(String path)
        {
            StringBuilder str = new StringBuilder("");
            if (System.IO.Directory.Exists(path))
            {
                //所有子文件名
                string[] files = System.IO.Directory.GetFiles(path);
                foreach (string file in files)
                    str.Append((str.Length == 0 ? "" : ";") + file);

                //所有子目录名
                string[] Dirs = System.IO.Directory.GetDirectories(path);
                foreach (string dir in Dirs)
                {
                    string tmp = getAllFiles(dir);  //子目录下所有子文件名
                    if (!tmp.Equals("")) str.Append((str.Length == 0 ? "" : ";") + tmp);
                }
            }
            return str.ToString();
        }


        //=======================================================
        //获取文件中的数据
        //=======================================================

        /// <summary>
        /// 获取文件中的数据串
        /// </summary>
        public string ToString(String filePath)
        {
            string str = "";

            //获取文件内容
            if (System.IO.File.Exists(filePath))
            {
                System.IO.StreamReader file1 = new System.IO.StreamReader(filePath);//读取文件中的数据
                str = file1.ReadToEnd();                                            //读取文件中的全部数据

                file1.Close();
                file1.Dispose();
            }
            return str;
        }

        //=======================================================
        //保存数据到文件
        //=======================================================
        /// <summary>
        /// 保存数据data到文件,返回值为保存的文件名
        /// </summary>
        public string SaveToFile(String data, String name, bool mute)
        {
            String filePath = "";

            //若未命名,则使用系统时间自动命名
            if (name == null || name.Trim().Equals("(重命名)") || name.Trim().Equals(""))
            {
                name = DateTime.Now.ToLongTimeString().Replace(":", ".");   //使用系统时间自动为文件命名
                filePath = SaveToFile(data, name, false);                   //保存数据到文件
                return filePath;                                            //返回保存的文件名
            }

            try
            {
                filePath = SaveProcess(data, name);                         //保存数据并记录文件完整路径

                if (!mute) MessageBox.Show("成功导出数据到:“" + filePath + "”!");
                return filePath;
            }
            catch (Exception)
            {
                MessageBox.Show("保存数据失败");
                return "";
            }
        }

        /// <summary>
        /// 保存数据data到文件处理过程,返回值为保存的文件名
        /// </summary>
        public String SaveProcess(String data, String name)
        {
            string CurDir = System.AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.Date.ToString("yyyy_MM_dd") + @"(CodeReplace)导出\";         //设置当前目录
            if (!System.IO.Directory.Exists(CurDir)) System.IO.Directory.CreateDirectory(CurDir);   //该路径不存在时,在当前文件目录下创建文件夹"导出.."

            //不存在该文件时先创建
            String filePath = CurDir + name + ".txt";
            System.IO.StreamWriter file1 = new System.IO.StreamWriter(filePath, false);     //文件已覆盖方式添加内容

            file1.Write(data);                                                              //保存数据到文件

            file1.Close();                                                                  //关闭文件
            file1.Dispose();                                                                //释放对象

            return filePath;
        }

        /// <summary>
        /// 保存数据data到原文件filePathName中
        /// </summary>
        public String SaveToNativeFile(String data, String filePathName, bool mute)
        {
            try
            {
                System.IO.StreamWriter file1 = new System.IO.StreamWriter(filePathName, false); //文件已覆盖方式添加内容
                file1.Write(data);                                                              //保存数据到文件

                file1.Close();                                                                  //关闭文件
                file1.Dispose();                                                                //释放对象

                if (!mute) MessageBox.Show("成功导出数据到:“" + filePathName + "”!");
                return filePathName;
            }
            catch (Exception)
            {
                return SaveToFile(data, "", mute);          //若保存到原文件失败,则创建新文件进行保存
            }
        }


        //=======================================================
        // 提示信息
        //=======================================================

        /// <summary>
        /// 显示提示信息str,并选择是否打开文件目录Dir
        /// </summary>
        public void MessageWithOpen(String str, String Dir)
        {
            bool ok = (MessageBox.Show(str, "打开?", MessageBoxButtons.OKCancel) == DialogResult.OK);
            if (ok) System.Diagnostics.Process.Start("explorer.exe", "/e,/select, " + Dir);
        }
    }
}
namespace CodeReplace
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.button2 = new System.Windows.Forms.Button();
            this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
            this.button3 = new System.Windows.Forms.Button();
            this.checkBox1 = new System.Windows.Forms.CheckBox();
            this.button4 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(14, 142);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "替换";
            this.toolTip1.SetToolTip(this.button1, "按给定的格式智能替换,通配符号*");
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(93, 46);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(199, 21);
            this.textBox1.TabIndex = 1;
            this.textBox1.Text = "R.id.*";
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(93, 70);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(199, 21);
            this.textBox2.TabIndex = 2;
            this.textBox2.Text = "R.id(\"*\")";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(9, 46);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(35, 12);
            this.label1.TabIndex = 3;
            this.label1.Text = "Find:";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(9, 73);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(83, 12);
            this.label2.TabIndex = 4;
            this.label2.Text = "Replace With:";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(9, 9);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(41, 12);
            this.label3.TabIndex = 5;
            this.label3.Text = "label3";
            this.label3.Click += new System.EventHandler(this.label3_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(136, 142);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(121, 23);
            this.button2.TabIndex = 6;
            this.button2.Text = "R资源id自定义替换";
            this.toolTip1.SetToolTip(this.button2, "特定的替换逻辑,用于替换安卓中R资源id,\r\n的静态调用,为动态获取");
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(298, 70);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(69, 23);
            this.button3.TabIndex = 8;
            this.button3.Text = "导出R文件";
            this.toolTip1.SetToolTip(this.button3, "导出预设R文件至该路径");
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // checkBox1
            // 
            this.checkBox1.AutoSize = true;
            this.checkBox1.Location = new System.Drawing.Point(263, 146);
            this.checkBox1.Name = "checkBox1";
            this.checkBox1.Size = new System.Drawing.Size(30, 16);
            this.checkBox1.TabIndex = 7;
            this.checkBox1.Text = "R";
            this.checkBox1.UseVisualStyleBackColor = true;
            this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
            // 
            // button4
            // 
            this.button4.Location = new System.Drawing.Point(299, 142);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(57, 23);
            this.button4.TabIndex = 9;
            this.button4.Text = "逆替换";
            this.toolTip1.SetToolTip(this.button4, "还原\"R自定义替换操作\"\r\n执行逆向替换逻辑");
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new System.EventHandler(this.button4_Click);
            // 
            // Form1
            // 
            this.AllowDrop = true;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(367, 185);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.checkBox1);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "字符串批量替换";
            this.toolTip1.SetToolTip(this, "拖动源码文件或文件夹至此");
            this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop);
            this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.ToolTip toolTip1;
        private System.Windows.Forms.CheckBox checkBox1;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button4;
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeReplace
{
    public class Data
    {
        public static string R_java = "package com.shjc.jsbc.view2d.util;\r\n\r\nimport java.lang.reflect.Field;\r\n\r\nimport android.util.Log;\r\n\r\n/**\r\n * 根据资源名称,动态获取资源id;\r\n * 替换此文件中的com.joym.BigSonGoCart.R为对应工程的R资源,修改工程中所有对于R资源的引用为当前类函数\r\n * @author wangzhongyuan 2015-11-30\r\n */\r\npublic class R\r\n{\t\r\n\tpublic static int drawable(String name)\r\n\t{\r\n\t\tint id = -1;\r\n\t\ttry\r\n\t\t{\r\n\t\t\tField field = com.joym.BigSonGoCart.R.drawable.class.getField(name);\r\n\t\t\tid = field.getInt(new com.joym.BigSonGoCart.R.drawable());\r\n\t\t}\r\n\t\tcatch (Exception e)\r\n\t\t{\r\n\t\t\tLog(\"获取资源id异常!\");\r\n\t\t}\r\n\t\t\r\n\t\treturn id;\r\n\t}\r\n\t\r\n\tpublic static int id(String name)\r\n\t{\r\n\t\tint id = -1;\r\n\t\ttry\r\n\t\t{\r\n\t\t\tField field = com.joym.BigSonGoCart.R.id.class.getField(name);\r\n\t\t\tid = field.getInt(new com.joym.BigSonGoCart.R.id());\r\n\t\t}\r\n\t\tcatch (Exception e)\r\n\t\t{\r\n\t\t\tLog(\"获取资源id异常!\");\r\n\t\t}\r\n\t\t\r\n\t\treturn id;\r\n\t}\r\n\t\r\n\tpublic static int layout(String name)\r\n\t{\r\n\t\tint id = -1;\r\n\t\ttry\r\n\t\t{\r\n\t\t\tField field = com.joym.BigSonGoCart.R.layout.class.getField(name);\r\n\t\t\tid = field.getInt(new com.joym.BigSonGoCart.R.layout());\r\n\t\t}\r\n\t\tcatch (Exception e)\r\n\t\t{\r\n\t\t\tLog(\"获取资源id异常!\");\r\n\t\t}\r\n\t\t\r\n\t\treturn id;\r\n\t}\r\n\t\r\n\tpublic static int raw(String name)\r\n\t{\r\n\t\tint id = -1;\r\n\t\ttry\r\n\t\t{\r\n\t\t\tField field = com.joym.BigSonGoCart.R.raw.class.getField(name);\r\n\t\t\tid = field.getInt(new com.joym.BigSonGoCart.R.raw());\r\n\t\t}\r\n\t\tcatch (Exception e)\r\n\t\t{\r\n\t\t\tLog(\"获取资源id异常!\");\r\n\t\t}\r\n\t\t\r\n\t\treturn id;\r\n\t}\r\n\t\r\n\tpublic static int string(String name)\r\n\t{\r\n\t\tint id = -1;\r\n\t\ttry\r\n\t\t{\r\n\t\t\tField field = com.joym.BigSonGoCart.R.string.class.getField(name);\r\n\t\t\tid = field.getInt(new com.joym.BigSonGoCart.R.string());\r\n\t\t}\r\n\t\tcatch (Exception e)\r\n\t\t{\r\n\t\t\tLog(\"获取资源id异常!\");\r\n\t\t}\r\n\t\t\r\n\t\treturn id;\r\n\t}\r\n\t\r\n\tpublic static int style(String name)\r\n\t{\r\n\t\tint id = -1;\r\n\t\ttry\r\n\t\t{\r\n\t\t\tField field = com.joym.BigSonGoCart.R.style.class.getField(name);\r\n\t\t\tid = field.getInt(new com.joym.BigSonGoCart.R.style());\r\n\t\t}\r\n\t\tcatch (Exception e)\r\n\t\t{\r\n\t\t\tLog(\"获取资源id异常!\");\r\n\t\t}\r\n\t\t\r\n\t\treturn id;\r\n\t}\r\n\t\r\n\tpublic static int menu(String name)\r\n\t{\r\n\t\tint id = -1;\r\n\t\ttry\r\n\t\t{\r\n\t\t\tField field = com.joym.BigSonGoCart.R.menu.class.getField(name);\r\n\t\t\tid = field.getInt(new com.joym.BigSonGoCart.R.menu());\r\n\t\t}\r\n\t\tcatch (Exception e)\r\n\t\t{\r\n\t\t\tLog(\"获取资源id异常!\");\r\n\t\t}\r\n\t\t\r\n\t\treturn id;\r\n\t}\r\n\t\r\n\tpublic static int anim(String name)\r\n\t{\r\n\t\tint id = -1;\r\n\t\ttry\r\n\t\t{\r\n\t\t\tField field = com.joym.BigSonGoCart.R.anim.class.getField(name);\r\n\t\t\tid = field.getInt(new com.joym.BigSonGoCart.R.anim());\r\n\t\t}\r\n\t\tcatch (Exception e)\r\n\t\t{\r\n\t\t\tLog(\"获取资源id异常!\");\r\n\t\t}\r\n\t\t\r\n\t\treturn id;\r\n\t}\r\n\t\r\n\tpublic static int array(String name)\r\n\t{\r\n\t\tint id = -1;\r\n\t\ttry\r\n\t\t{\r\n\t\t\tField field = com.joym.BigSonGoCart.R.array.class.getField(name);\r\n\t\t\tid = field.getInt(new com.joym.BigSonGoCart.R.array());\r\n\t\t}\r\n\t\tcatch (Exception e)\r\n\t\t{\r\n\t\t\tLog(\"获取资源id异常!\");\r\n\t\t}\r\n\t\t\r\n\t\treturn id;\r\n\t}\r\n\t\r\n\tpublic static int attr(String name)\r\n\t{\r\n\t\tint id = -1;\r\n\t\ttry\r\n\t\t{\r\n\t\t\tField field = com.joym.BigSonGoCart.R.attr.class.getField(name);\r\n\t\t\tid = field.getInt(new com.joym.BigSonGoCart.R.attr());\r\n\t\t}\r\n\t\tcatch (Exception e)\r\n\t\t{\r\n\t\t\tLog(\"获取资源id异常!\");\r\n\t\t}\r\n\t\t\r\n\t\treturn id;\r\n\t}\r\n\t\r\n\t//import java.lang.reflect.Field;\r\n\t/** 获取Object对象,所有成员变量属性值 */\r\n\tpublic static void getObjAttr(Object obj)\r\n\t{\r\n\t\t// 获取对象obj的所有属性域\r\n\t\tField[] fields = obj.getClass().getDeclaredFields();\r\n\t\t\r\n\t\tfor (Field field : fields)\r\n\t\t{\r\n\t\t\t// 对于每个属性,获取属性名\r\n\t\t\tString varName = field.getName();\r\n\t\t\ttry\r\n\t\t\t{\r\n\t\t\t\tboolean access = field.isAccessible();\r\n\t\t\t\tif(!access) field.setAccessible(true);\r\n\t\t\t\t\r\n\t\t\t\t//从obj中获取field变量\r\n\t\t\t\tObject o = field.get(obj);\r\n\t\t\t\tSystem.out.println(\"变量: \" + varName + \" = \" + o);\r\n\t\t\t\t\r\n\t\t\t\tif(!access) field.setAccessible(false);\r\n\t\t\t}\r\n\t\t\tcatch (Exception ex)\r\n\t\t\t{\r\n\t\t\t\tex.printStackTrace();\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\t// 当try块出错时,给出当前代码位置的,出错提示信息\r\n\t// try{}catch(Exception e){ Log(\"释放资源出错!\"); }\r\n\t\r\n\t/** 在LogCat中输出提示信息info,并给出输出该信息在代码中的调用位置 */\r\n\tpublic static void Log(String info)\r\n\t{\r\n\t\tStackTraceElement[] elem = Thread.currentThread().getStackTrace();\t// 从当前位置,获取代码的堆栈追踪信息\r\n\t\t\r\n\t\tStackTraceElement e = elem[3];\t\t\t\t\t\t\t\t\t\t// 仅获取调用该函数的代码位置,不获取完整调用树\r\n\t\tString str = info + \" 位置:\" + e.getClassName() + \" -> \" + e.getMethodName() + \"() 行:\" + e.getLineNumber();\r\n\t\t// Gdx.app.error(e.getFileName(), str); // 输出调用位置信息和提示信息\r\n\t\tLog.e(info, str);\r\n\t\t// System.out.println(info + \"  \" + str);\r\n\t}\r\n\r\n}\r\n";
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值