简体   繁体   English

如何使用Java代码将string.xml中的键值对String数组导入TextView

[英]How to import key-value pair String array from strings.xml into TextView using Java Code

Alright , heres the code 好了,这是代码

Heres Java file 继承人的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

}

} }

and heres the activity_main.xml 这是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>

heres string.xml . 继承人string.xml。 the array contains key value pairs. 该数组包含键值对。

<?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>

and heres the image . 这就是形象。 键将在Text1中,值将在图像Text2中

Now, what I need to do is, When I click on the Button, It will cycle through array key-value pairs defined in strings.xml and will display key in textview1 and corresponding value in textview2. 现在,我需要做的是,当我单击Button时,它将循环显示在strings.xml中定义的数组键/值对,并在textview1中显示键,在textview2中显示对应的值。

If you know how to do it, please paste code here. 如果您知道该怎么做,请在此处粘贴代码。 Or may be give me instructions. 或者可以给我指示。

Thanks in advance 提前致谢

This is how you get the value from the 这就是您从中获得价值的方式 defined in xml: 在xml中定义:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM