android textview 显示 文本 .txt
把 .txt文件 放在 assets文件夹中
public class MainActivity extends Activity {
public static final String ENCODING = "GBK";private TextView teView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
teView = (TextView) findViewById(R.id.button1);
editText2 = (EditText) findViewById(R.id.editText2);
intent_two = (Button) findViewById(R.id.intent_two);
teView.setText(getFromAssets("xieyi.txt"));
public String getFromAssets(String fileName) {
String result = "";
try {
InputStream in = getResources().getAssets().open(fileName);
// 获取文件的字节数
int lenght = in.available();
// 创建byte数组
byte[] buffer = new byte[lenght];
// 将文件中的数据读到byte数组中
in.read(buffer);
result = EncodingUtils.getString(buffer, ENCODING);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
<?xml version="1.0" encoding="UTF-8"?>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:orientation="vertical" >
android:layout_height="fill_parent"
android:layout_marginTop="100dp" >
android:layout_height="wrap_content" >
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
更多相关文章
- 【Android】 Android中适配器简介
- android日期控件显示
- android java 检测文件夹(目录)是否存在,不存在则创建
- Android(安卓)zip文件压缩解压缩
- android截取屏幕图片
- make: *** [out/host/linux-x86/obj/EXECUTABLES/emulator_inter
- Android常用功能代码块
- 箭头函数的基础使用
- NPM 和webpack 的基础使用