getResources().里有很多种方法,可以用有道翻译看下。我就不多说了,思路有很多种,每个人的习惯都不一样。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn_main);
tv = (TextView) findViewById(R.id.tv_main);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
// 1.读取strings.xml 里的jijing字符串
// String str = getResources().getString(R.string.jijing);
// 2.读取assets文件里的str.xml文件
// String str = Inputstr2Str_Reader(getResources().getAssets().open("str.xml"), "utf-8");
// 3.读取values里的ooo.xml文件里的
//ooo.xml文件里的内容
//<?xml version="1.0" encoding="UTF-8"?>
//<resources>
// <string-array name="stockbaseinfotable">
// <item>360001,9911,鹏华深证民营,PHSZMY,1,0;206010,鹏华深证民营联接,PHSZMYLJ,1,1;</item>
// </string-array>
//</resources>
String[] str = getResources().getStringArray(R.array.stockbaseinfotable);
tv.setText(str[0]);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public static String Inputstr2Str_Reader(InputStream in, String encode) {
String str = "";
try {
if (encode == null || encode.equals("")) {
// 默认以utf-8形式
encode = "utf-8";
}
BufferedReader reader = new BufferedReader(new InputStreamReader(
in, encode));
StringBuffer sb = new StringBuffer();
while ((str = reader.readLine()) != null) {
sb.append(str).append("\n");
}
return sb.toString();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}