gpt4 book ai didi

java - 如何使用 Java 代码将键值对字符串数组从 strings.xml 导入 TextView

转载 作者:行者123 更新时间:2023-12-01 12:42:35 25 4
gpt4 key购买 nike

好的,这是代码

这是 Java 文件

public class MainActivity extends Activity implements OnClickListener {
private String array1;
private String [] arraymain;
TextView tv1,tv2;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
//getFragmentManager().beginTransaction()
// .add(R.id.container, new PlaceholderFragment()).commit();
}
tv1=(TextView)findViewById(R.id.maintv1);
tv2=(TextView)findViewById(R.id.maintv2);
btn=(Button)findViewById(R.id.mainbutton1);
btn.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}

}

这是activity_main.xml

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>


<TextView
android:id="@+id/maintv1"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Text here"
android:textSize="24dp"
/>

<TextView
android:id="@+id/maintv2"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Text here"
android:textSize="24dp"
/>

<Button
android:id="@+id/mainbutton1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Click here"
/>

</LinearLayout>

这里是 string.xml 。该数组包含键值对。

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">KeyValuePairDemo</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>

<string-array name="array1" >
<item name="a1">Hello</item>
<item name="a2">Hola</item>
<item name="a2">Helios</item>
</string-array>

</resources>

这是图片。 Key will be in Text1 , Value will be in image Text2

现在,我需要做的是,当我单击按钮时,它将循环遍历 strings.xml 中定义的数组键值对,并在 textview1 中显示键并在 textview2 中显示相应的值。

如果您知道如何操作,请在此处粘贴代码。或者可以给我指示。

提前致谢

最佳答案

这是从 xml 中定义的 获取值的方法:

String[] arrayXml= yourContext.getResources().getStringArray(R.array.my_array);
textView.setText(arrayXml[0]);//setText

关于java - 如何使用 Java 代码将键值对字符串数组从 strings.xml 导入 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24970782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com