Android:如何以愉快的方式在多个页面上显示文本(viewflipper)

发布于 2024-12-04 09:20:20 字数 2578 浏览 2 评论 0原文

我需要为考试创建一个电子书阅读器:

当我们单击文件时..

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回梦 2024-12-11 09:20:20
stringBuilder.append(readString).append("\n");
stringBuilder.append(readString).append("\n");
遗失的美好 2024-12-11 09:20:20

这是我的更酷的方式

,这是使用 ViewPagerPagerAdapter 的代码

    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;
        }
    }

}

HERE IS MY WAY COOLER WAY

here is A code USING ViewPager and 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;
        }
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文