我需要为考试创建一个电子书阅读器:
当我们点击文件时..
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(我的页面)中可以容纳多少文字?
谢谢
答案 0 :(得分:0)
stringBuilder.append(readString).append("\n");
答案 1 :(得分:0)
这是我的方式冷却方式
这是使用ViewPager
和PagerAdapter
MyBookReader extends Activity{
SectionPagerAdapter mySectionPagerAdapter;
LayoutInflater inflater;
ViewPager mViewPager;
String[] book;
//total book pages
int totalLength;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//inflate individual pages
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//JUST FOR FUN ...I DECIDE NOT TO WRITE ON HOW TO BRING CONTENT
//AND MANIPULATE IT INTO array of string objects
//**tip put it into raw data and bring it
//*** last tip ... nope not saying it :-)
// ITS WALK IN A PARK AFTER THIS
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.viewPager);
mViewPager.setAdapter(new SectionsPagerAdapter());
}
public class SectionsPagerAdapter extends PagerAdapter {
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
View page = inflater.inflate(R.layout.fragment_main, null);
//manipulate your book content into array of book strings ((TextView)page.findViewById(R.id.section_label)).setText(book[position]);
((ViewPager) container).addView(page, 0);
return page;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
//See if object from instantiateItem is related to the given view
//required by API
return arg0==(View)arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
object=null;
}
@Override
public int getCount() {
// Show total pages.
//number of pages **** getResource.screen size....
return totalLength;
}
}
}