将所有硬编码的字符串提取到字符串资源 [英] Extract all hardcoded strings to string resource

查看:77
本文介绍了将所有硬编码的字符串提取到字符串资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有 Alt + Enter 组合可以将单个String提取为字符串资源.但是我想知道是否有什么可以将项目的所有字符串提取到字符串资源中的?

如果我使用其中之一,Android Studio也不会将相同的String转换为字符串资源.

String s = "Hello World";
String s2 = "Hello World";

例如.我也将"Hello World"作为字符串资源,并将其保留在项目中的另一个硬编码中.

String s = getString(R.string.helloworld);
String s2 = "Hello World";

如果有人知道这样的话.

解决方案

根据您的要求,并且我知道android studio中没有您真正要寻找的功能,但是这里有一些替代方法可以为您提供帮助. /p>

  • 转到"Analyze> Run Inspection ..",然后键入"Hardcoded strings".在整个项目中运行它,您将获得一个检查结果面板,该面板将显示项目的所有硬编码文本.然后按 Alt + Enter ,您将获得一个自动提取该字符串的选项.

  • 另一种方法是查找并替换,但这并不好,因为会浪费时间.为了简化方法,您可以在此处的灵活性.

I know there is Alt + Enter combination to extract single String to string resource. But I am wondering is there anything that I can extract all string of my project into string resource?

Also Android studio not making same String to string resource if I make one of them.

String s = "Hello World";
String s2 = "Hello World";

For example. I make "Hello World" to string resource still another remain Hardcoded in same file as well in the project too.

String s = getString(R.string.helloworld);
String s2 = "Hello World";

If anyone know something like that.

解决方案

As your requirements and as I know there is no such feature in android studio you were really searching for, But here are some alternative ways that can help you.

  • Go to "Analyze > Run Inspection ..", and type "Hardcoded strings". Run that in your whole project, and you will get an inspection results panel that will show all the hardcoded text of projects. Then hit Alt + Enter and you'll get an option to automatically extract that Strings.

  • Another approach is to Find and Replace But It's not better because of time consumption. To simplify the approach you can have a look at here for flexibility.

这篇关于将所有硬编码的字符串提取到字符串资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆