intent应用——文本管理器功能实现

1、首先,在之前的文件管理器的基础上,加入只显示txt文件的验证功能。

// 如果是文件夹,可以直接加入,如果是文件,需要判断必须扩展名为txt
				if (f.isDirectory() || (map.get("ext").equals("txt"))) {
					// 将map集合加入到list中
					allValues.add(map);
				}

当点某一项时,需要通过Intent,将该文件的完整路径,以及文件名信息,传递到下一个Activity中。

因此需要建立一个Activity,这里命名为DetailActivity。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff" >

    <TextView
        android:id="@+id/detail_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="14sp" />

</ScrollView>

实现切换操作

// 切换到下一个界面
					Intent in = new Intent(MainActivity.this,
							DetailActivity.class);
					// 传递参数
					in.putExtra("fullPath", map.get("fullPath").toString());

					startActivity(in);

在DetailActivity中接收数据,并进行IO流读取操作,将读取到的内容加入到TextView里显示。

public class DetailActivity extends Activity {

	private TextView detailText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail);

		detailText = (TextView) findViewById(R.id.detail_text);

		// 取得数据
		String path = getIntent().getStringExtra("fullPath");
		// 建立文件
		File file = new File(path);

		// 读取
		try {
			BufferedReader reader = new BufferedReader(new FileReader(file));
			String line = null;
			// 保存所有文本的StringBuilder
			StringBuilder builder = new StringBuilder();
			while ((line = reader.readLine()) != null) {
				builder.append(line + "\r\n");
			}
			reader.close();

			// 将数据设置到TextView里
			detailText.setText(builder.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

由于会出现乱码,因此要对内容进行编码处理。

// 需要使用InputStreamReader对读入的文件内容进行转码,前提文本是GBK编码就必须转码.
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(file),"GBK"));





































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值