Android实战,实现文本读取,实现assets下资源文本的读取,实现l列表下隐藏TextView点击显示

实现Android读取assets资源目录下文本,实例的效果:

1,首先,是一个主界面

2,点击指示的位置,进入下一页面,通过实现点击列别,弹出对应问题的对应的答案。

 

一、实现上述的效果,首先要有资源可供读取,这就要在Android assets下创建一个文本文件,由于上述我们实现的效果是问题列表的形式,这就需要我们在创建文本时按照一定的模式去创建,当然这不是开发人员去做的,所以这种格式要求尽量的简便:

QUESTION_START_FLAG
1 、一个 ".java" 源文件中是否可以包括多个类(不是内部类)?有什么限制? 
ANSWER_START_FLAG
可以有多个类,但只能有一个 public 的类,并且 public 的类名必须与文件名相一致。
END_FLAG

文本中,问题的开始我们加入QUESTION_START_FLAG特殊字符串,答案的开始加入ANSWER_START_FLAG为特殊字符串,答案的结束以END_FLAG标明。

以这样的格式完成文本的创建。

 

二、编写第一个布局文件activity_main.xml,这里我用的是包含页,将头部activity_head.xml与尾部activity_doot.xml分开。

这是activity_main.xml

<LinearLayout 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="@drawable/main_bg"
    android:orientation="vertical" >

    <!-- 顶部 -->

	<include layout="@layout/activity_head"/>
    <!-- 中间 -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="670"
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="80" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="205"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/book" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />
        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="52" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="40"
            android:gravity="center"
            android:text="欢迎使用"
            android:textColor="#ff0000"
            android:textSize="20sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="80" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="103"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="37" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="406"
                android:background="@drawable/main_icon" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="37" />
        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="95" />
    </LinearLayout>

    <!-- 底部 -->
	<include layout="@layout/activity_foot"/>
</LinearLayout>


activity_head.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="50"
    android:background="@drawable/head_bg"
    android:orientation="horizontal"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp" >

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="7.8"
        android:gravity="center"
        android:text="程序员面试宝典"
        android:textColor="#ffffff"
        android:textSize="16sp" />

    <Button
        android:id="@+id/setting_btn"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/head_back_btn"
        android:paddingBottom="5dp"
        android:paddingRight="5dp"
        android:paddingTop="5dp" />

</LinearLayout>

这是activity_foot.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="80"
    android:orientation="horizontal"
    android:paddingBottom="10dp"
    android:paddingTop="10dp" >

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3.5"
        android:background="@drawable/buttom_bg"
        android:orientation="horizontal"
        android:paddingBottom="5dp"
        android:paddingTop="5dp" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5" />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/bottom_btn01_b" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1.5" />

        <Button
            android:id="@+id/list_btn"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/bottom_btn02_b" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1.5" />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/bottom_btn03_b" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5" />
    </LinearLayout>

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

注:这里的单个的 head 和 foot是演示不了的,因为我采用上中下权值比例布局的。整体在main中可以演示的。

三、下面实现的列表的activity_question_list样式

<LinearLayout 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:orientation="vertical"
    android:background="@drawable/main_bg" >
	<include layout="@layout/activity_head"/>
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="670"
        android:orientation="vertical"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="5dp">
        
          <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/list_bg"
            android:cacheColorHint="#00000000"
            android:divider="#cccccc"
            android:dividerHeight="1dp"
            android:paddingBottom="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingTop="5dp" >
        </ListView>
    </LinearLayout>
    	<include layout="@layout/activity_foot"/>
</LinearLayout>

这是list主页面,还有activity_question_line,主要是实现对列表页面的数据填充。

这里只是一个TextView组件,用以显示问题,至于答案,要做到点击显示,再点击消失,就必须是动态生成的。

 

四、这里我们没有用到数据库,就建立一个公共类属性List<Map<String,Object>>   allValues

import java.util.List;
import java.util.Map;

import android.app.Activity;

public class Globals {

	public static int SCREEN_WIDTH;
	public static int SCREEN_HEIGHT;
	
	public static List<Map<String,Object>> allValues;
	
	public static void init(Activity a){
		SCREEN_WIDTH = a.getWindowManager().getDefaultDisplay().getWidth();
		SCREEN_HEIGHT= a.getWindowManager().getDefaultDisplay().getHeight();
	}
}


 SCREEN_WIDTH 和 SCREEN_HEIGHT分别是获得手机的高度和宽度,按比例压缩扩展整体布局。

 

五、建立IndexActivity,用以实现页面加载,和数据读取,(通常一些软件判断是否连网时,都是通过这一页面验证的)

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.example.testfourinterview.util.Globals;

public class IndexActivity extends Activity {

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

		// 加入线程,实现界面跳转
		Thread t = new Thread() {

			@Override
			public void run() {
				// 读取question.txt的内容
				// 取得assests目录下的文件内容
				try {
					InputStream is = getAssets().open("question.txt");
					BufferedReader reader = new BufferedReader(
							new InputStreamReader(is, "GBK"));
					// 逐行读取数据,这里读取到的数据传递给静态的allValues
					Globals.allValues = getDataFromFile(reader);
					reader.close();

					Thread.sleep(1000);
				} catch (Exception e) {
					e.printStackTrace();
				}
				// 切换界面
				Intent in = new Intent(IndexActivity.this, MainActivity.class);
				startActivity(in);
				// 切换后将自己关闭
				finish();
			}

		};
		t.start();
	}

	private List<Map<String, Object>> getDataFromFile(BufferedReader reader)
			throws Exception {

		List<Map<String, Object>> allValues = new ArrayList<Map<String, Object>>();

		String line = null;
		StringBuilder question = new StringBuilder();
		StringBuilder answer = new StringBuilder();
		// 加入之前是读取问题还是读取答案
		boolean questionFlag = false;
		while ((line = reader.readLine()) != null) {
			// 准备开始读取问题
			if ("QUESTION_START_FLAG".equals(line)) {
				questionFlag = true;
			} else if ("ANSWER_START_FLAG".equals(line)) {
				// 准备开始读取答案
				questionFlag = false;
				answer.append("  ");
			} else if ("END_FLAG".equals(line)) {
				// 需要将问题答案整合,并保存到List集合里
				// 将数据集体放入到Map,再加入到List里
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("question", question.toString());
				map.put("answer", answer.toString());
				allValues.add(map);

				// 清空问题和答案
				question = new StringBuilder();
				answer = new StringBuilder();
			} else {
				// 读取问题或答案数据,加入到Builder中
				if (questionFlag) {
					question.append(line + "\r\n");

				} else {
					answer.append(line + "\r\n");
				}
			}
		}
		return allValues;
	}

}

 

下面是建立一个MainActivity,实现按钮点击跳转的操作,(这里的顶部的按钮本应该是封装继承单独的一个类得来的,因为本应用只是单一的页面,所以就没有封装)

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	private Button listbtn;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		listbtn = (Button) findViewById(R.id.list_btn);
		
		listbtn.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v){
				Intent in = new Intent(MainActivity.this,
						SecondActivity.class);
				
				startActivity(in);
			}
			
		});
	}
}

在建立一个SecondActivity这是加载的跳转页面,用以点击显示问题和答案列表

import com.example.testfourinterview.util.Globals;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;



public class SecondActivity extends Activity {

	private ListView list;
	private SimpleAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_question_list);
	
	list = (ListView) findViewById(R.id.list);
	
	adapter = new SimpleAdapter(this,Globals.allValues,
			R.layout.activity_question_line,new String[]{"question"},
			new int[]{R.id.line_question});
	
	list.setAdapter(adapter);
	
	//点完显示的功能
		list.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3){
			
				// arg1就是当前所点的行对应的界面组件, 在这里就是LinearLayout
				LinearLayout line = (LinearLayout) arg1;
				// 取得第二个子节点
				// TextView text = (TextView) line.getChildAt(1);
				// text.setVisibility(View.VISIBLE);
				// 判断有几个子节点
				if(line.getChildCount() == 1){

					// 建立第二个子节点
					TextView text = new TextView(SecondActivity.this);
					text.setText(Globals.allValues.get(arg2).get("answer")
							.toString());
					text.setTextColor(Color.RED);
					text.setTextSize(14);
					// 加入到line中
					line.addView(text);
				} else {
					// 删除答案
					line.removeViewAt(1);
				}
			}
		});
	}
}


案例完成,需要素材的,可以加我微薄

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值