Android 采用线程方法动态显示文字
来源:互联网 发布:淘宝火车票网上订票 编辑:程序博客网 时间:2024/06/03 05:54
今天在学习Android的时候,不知怎么就突然想到了一个动态跳出显示内容的法子,感觉不难,想实现它,搜了搜资料,就完成了一个使句子中的字一个个跳出的Demo如下图所示:
句子太长截取了其中一部分
实现此功能需要实现两个步骤:
1 读文件,把文件中的内容显示在组件上
<span style="font-size:18px;">package com.example.jumpword;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import android.util.Log;/** * 按字节读 , 按字符读, 按行读 * @author jsjxy * */public class readword { //处理字节流的超类 /*inputstream , outputstream; //处理字符流的抽象类,(但是会把把字符流转化为字节流) inputstreamreader, outputstreamwriter*/ /* 缓冲方式文本读取,缓冲方式文本写入 * 从输入流中读取文本, 从文本写入输出流 */ //BufferedReader , BufferedWriter private static final String TAG = null;File file ; //得到地址 InputStreamReader inputstream; //把内存中的内容解析出来 FileInputStream fileinputstream; //把地址所指向的文件内容读取到内存中 BufferedReader reader ; //输出,转化为IO能够识别的数据 int word; String sentence = ""; public readword(){ } //读txt文件的位置,从txt文件中每次读一个字符 public String readword (String path) throws IOException{ file = new File(path); inputstream = new InputStreamReader(new FileInputStream(file),"GBK"); reader = new BufferedReader(inputstream); while(( word = reader.read() )!=-1){ // System.out.println((char) word); //每次读一个字符 Log.i(TAG, (char)word+""); sentence += (char)word; } return sentence; } //写txt文件 public void writeword(String path) throws IOException{ file = new File(path); FileOutputStream outputstream = new FileOutputStream(file); String content = "写入文件的方法"; outputstream.write(content.getBytes()); } }</span>
详细的读写文件的方法如下地址:http://www.cnblogs.com/lovebread/archive/2009/11/23/1609122.html(转别人博客写的) ,在此我不做详细的解释。
2 线程实现间隔时间跳出文字
package com.example.jumpword;import java.io.File;import java.io.IOException;import android.support.v7.app.ActionBarActivity;import android.app.Activity;import android.os.Bundle;import android.os.Environment;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.widget.TextView;public class MainActivity extends Activity { protected static final String TAG = null;private TextView Word; // 显示一个字 private readword read = new readword(); //读取文件的对象 private File file; private String sentence; //返回的读取信息 private String jiequchuan ; //截取的字符串 private int i = 1; //截取的位置索引 @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); file = Environment.getExternalStorageDirectory(); //地址相当于 “/sdcard.....”try { sentence = read.readword(file + "/hhh.txt");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} Word = (TextView)findViewById(R.id.showword); new Thread(new Runnable() { @Overridepublic void run() {// TODO Auto-generated method stub while(i < sentence.length()) { try {Thread.sleep(300);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();} jiequchuan = sentence.substring(0, i); //截取字符串方法,从0位置到i位置 runOnUiThread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stub Word.setText(jiequchuan);}}); i++; }}}).start(); }@Overridepublic 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;}@Overridepublic 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);}}以上为实现的主要核心方法,源代码地址如下:http://download.csdn.net/detail/danielntz/9405914
0 0
- Android 采用线程方法动态显示文字
- Android实现ScrollView动态显示文字
- OpenGL采用显示列表显示文字完整范例
- OpenGL采用显示列表显示文字完整范例
- android采用SurfaceView实现文字滚动效果
- 按钮上文字动态显示
- Android下Opengl程序显示文字的简单方法
- Android 中部分文字高亮显示方法
- Android编程开发之TextView文字显示和修改方法
- Android中用TextView显示大量文字的方法
- android显示gif动态图的方法
- Android竖排显示文字
- android竖直显示文字
- 将文字简短显示方法
- Android 文字居中方法
- android中动态文字排版
- js实现文字动态显示效果
- jquery动态title和文字缩略显示
- Java笔记9:Spring简单Demo
- 【LeetCode OJ 258】Add Digits
- Android性能优化之Splash页应该这样设计
- python join字符连接函数的使用方法
- mysql报错问题解决MySQL server PID file could not be found!
- Android 采用线程方法动态显示文字
- Codeforces Educational round 5
- link cut tree学习小记
- Android中selector的使用
- 排序—交换排序(js实现)
- zzulioj--1858--单词翻转(模拟)
- 简单遗传算法实现(MATLAB)
- pyghon list指定位置插入字符的方法
- Spring MVC起步