我有两个textview1和textview2和一个button1,并且textview1包含一个包,例如“ com.package.name”,我想从textview1中获取该包的名称,并在textview2中显示它,我想我可以接受使用正则表达式,但我不知道如何使用正则表达式
textview1中的软件包可能需要更改的另一件事,请帮我,这是我第二次问同样的问题
textview1看起来像这样
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myapp2" >
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
使用getText()完成上述任务:
字符串值= textView2.getText()。toString();
textView1.setText(value);
并使用
获取软件包名称BuildConfig.APPLICATION_ID
例如:
textView1.setText(BuildConfig.APPLICATION_ID);
字符串值= textView1.getText()。toString(); textView2.setText(value);
答案 1 :(得分:0)
您的textview包含xml,您可以解析该xml并检索您需要的属性,可以看到如何从该link解析xml文本
答案 2 :(得分:0)
此方法将像textView1一样从XML字符串中检索 package ,请使用以下方法获取Package
Java
String getPackageNameFromXml(String s) {
try {
XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser myParser = xmlFactoryObject.newPullParser();
myParser.setInput(new StringReader(s));
int event = myParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
String name = myParser.getName();
switch (event) {
case XmlPullParser.START_TAG: {
if (name.toLowerCase().equals("manifest")) {
String packageName = myParser.getAttributeValue(null, "package");
if (packageName != null) {
return packageName;
}
}
}
break;
case XmlPullParser.END_TAG: {
}
break;
}
event = myParser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
科特林
fun getPackageNameFromXml(s: String): String {
try {
val xmlFactoryObject = XmlPullParserFactory.newInstance()
val myParser = xmlFactoryObject.newPullParser()
myParser.setInput(StringReader(s))
var event = myParser.eventType
while (event != XmlPullParser.END_DOCUMENT) {
val name = myParser.name
when (event) {
XmlPullParser.START_TAG -> {
if (name.toLowerCase() == "manifest") {
val packageName = myParser.getAttributeValue(null, "package")
if (packageName != null) {
return packageName
}
}
}
XmlPullParser.END_TAG -> {
}
}
event = myParser.next()
}
} catch (e: XmlPullParserException) {
e.printStackTrace()
} catch (e: IOException) {
e.printStackTrace()
}
return ""
}
只需如下调用此方法
textView2.setText(getPackageNameFromXml(textView1.getText().toString()))