Android:如何以愉快的方式在多个页面上显示文本(viewflipper)
我需要为考试创建一个电子书阅读器:
当我们单击文件时..
private void onFileClick(Option o){
Toast.makeText(this, "File Clicked: "+o.getName(), Toast.LENGTH_SHORT).show();
BookView.readBook(o);
Intent intent = new Intent(this,BookView.class);
this.startActivityForResult(intent, 1000);
}
然后我们读取文本文件的内容:
static void readBook(Option o){
try{
File f = new File(Environment.getExternalStorageDirectory()+"/"+o.getName());
FileInputStream fileIS = new FileInputStream(f);
BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
String readString = new String();
int i = 0;
monArrayList.clear();
stringBuilder = new StringBuilder();
while((readString = buf.readLine())!= null){
i=i+1;
int stringLength = stringBuilder.length();
stringBuilder.append(readString);
if(stringLength>=1040){
monArrayList.add(new String(stringBuilder));
stringBuilder.setLength(0);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}
然后我们开始活动:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
detector = new GestureDetector(this,this);
view1 = (ViewFlipper)findViewById(R.id.flipper);
Iterator<String> iterator = monArrayList.iterator();
while (iterator.hasNext()) {
String element = iterator.next();
view1.addView(createTextView(element));
}
view1.addView(createTextView(stringBuilder));
slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);
layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
BitmapDrawable bmp = (BitmapDrawable)this.getResources().getDrawable(R.drawable.back);
layout.setBackgroundDrawable(bmp);
layout.setPadding(20, 20, 20, 20);
}
目前它可以工作,但不太漂亮。我将记录限制为 1040 个字符,将书页保存到 ArrayList 中。
这些页面没有智能地完成(有些页面只有 3/4)。这是因为我的程序不包括换行和回车的检测。
我想使我的文本适应我的textView,以便它们重合。
我如何知道我的textView(我的页面)可以容纳多少文本?
谢谢
I need to create an eBook reader for an exam:
When we click on a file..
private void onFileClick(Option o){
Toast.makeText(this, "File Clicked: "+o.getName(), Toast.LENGTH_SHORT).show();
BookView.readBook(o);
Intent intent = new Intent(this,BookView.class);
this.startActivityForResult(intent, 1000);
}
Then we read the contents of text file:
static void readBook(Option o){
try{
File f = new File(Environment.getExternalStorageDirectory()+"/"+o.getName());
FileInputStream fileIS = new FileInputStream(f);
BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
String readString = new String();
int i = 0;
monArrayList.clear();
stringBuilder = new StringBuilder();
while((readString = buf.readLine())!= null){
i=i+1;
int stringLength = stringBuilder.length();
stringBuilder.append(readString);
if(stringLength>=1040){
monArrayList.add(new String(stringBuilder));
stringBuilder.setLength(0);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}
Then we start the activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
detector = new GestureDetector(this,this);
view1 = (ViewFlipper)findViewById(R.id.flipper);
Iterator<String> iterator = monArrayList.iterator();
while (iterator.hasNext()) {
String element = iterator.next();
view1.addView(createTextView(element));
}
view1.addView(createTextView(stringBuilder));
slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);
layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
BitmapDrawable bmp = (BitmapDrawable)this.getResources().getDrawable(R.drawable.back);
layout.setBackgroundDrawable(bmp);
layout.setPadding(20, 20, 20, 20);
}
Currently it works, but it is not pretty. I saved my book pages into an ArrayList by limiting the record to 1040 characters.
The pages are not fulfilled intelligently (some pages are at just 3/4). This is due to the fact that my program does not include detection of line feed and carriage return ..
I would like to adapt my text to my textView, so that they coincide.
How do I know how much text can fit in my textView (my page)?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我的更酷的方式
,这是使用
ViewPager
和PagerAdapter
的代码HERE IS MY WAY COOLER WAY
here is A code USING
ViewPager
andPagerAdapter