2014-07-26 109 views
0

好吧,继承人的代码如何从strings.xml中使用Java代码

继承人的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导入键值对字符串数组的TextView。 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。该数组包含键值对。

<?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相应价值的关键。

如果您知道如何操作,请将代码粘贴到此处。或者可能会给我说明。

在此先感谢

+0

看看[在Android的关键值的字符串数组(HTTP:// stackoverflow.com/questions/7256514/search-value-for-key-in-string-array-android) – Apoorv

回答

1

这是你如何让从在XML中定义的值:

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

如何从字符串中定义的项目数组中提取“名称字段到Java文件中? – Ajinkya

+1

@Ajinkya:关键'字符串数组实际上只是''。如果你想要一个键/值对,你需要连接也许使用CSV格式命名它们 - 例如'名称,Squonk <\item>'。然后你可以使用字符串'split()'方法来分离键和值。 – Squonk