停到某个关键字出再的地方,或者停到某一行。

解决方案 »

  1.   

    那还是那个计算位置的问题,
    算出有多少行文本,然后找到关键词在第几行,然后根据屏幕能显示的最大行数通过scrollto的方法来手动定位滚动条的位置。
      

  2.   


    public class LoadDocReader extends Activity
    {
    //  private ScrollView mScrollView;
      private TextView contentView;
      private LinearLayout mLayout; 
      private String docInfo;
      private String keyword;
      private int keywordNum;
      private ScrollView scrollview;
      private final Handler mHandler = new Handler(); 
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置标题栏带有进度条
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        String tag = "onCreate";
        Log.d(tag, "initialize the new Activity");
        setContentView(R.layout.reader);
        mLayout = (LinearLayout) this.findViewById(R.id.textLayout);
        /** the phone component initialization */
      //  mScrollView = (ScrollView) findViewById(R.id.scrollView);
        contentView = (TextView) findViewById(R.id.textContent);
        scrollview = (ScrollView)findViewById(R.id.scrollView);
      //  mLinearLayout = (LinearLayout) findViewById(R.id.textLayout);
        
        //显示操作正在进行
        loadData();
        //取消显示
      }
      /**
       * 载入书籍的数据
       */
      private void loadData() {
        
        Bundle bundle=getIntent().getExtras(); 
        docInfo=bundle.getString("docStr");
        String tempDocInfo = docInfo;
        keyword = bundle.getString("keyword");
        keywordNum = bundle.getInt("keywordNum");
        int keywordIndex = tempDocInfo.indexOf(keyword);
    //    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(  
    //        LinearLayout.LayoutParams.FILL_PARENT,  
    //        LinearLayout.LayoutParams.WRAP_CONTENT); 
        SpannableStringBuilder style=new SpannableStringBuilder(docInfo);
        while(keywordIndex!=-1)
        {
          style.setSpan(new BackgroundColorSpan(Color.RED),keywordIndex,keywordIndex+keyword.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
          int tempkeywordTempIndex = keywordIndex+keyword.length();
          //style.setSpan(new ForegroundColorSpan(Color.RED),keywordIndex,keywordIndex+keyword.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
          tempDocInfo = docInfo.substring(tempkeywordTempIndex,docInfo.length());
          keywordIndex = tempDocInfo.indexOf(keyword);
          if(keywordIndex!=-1)
          {
            keywordIndex = keywordIndex+tempkeywordTempIndex;
          }
        }
        contentView.setText(style);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(  
            LinearLayout.LayoutParams.FILL_PARENT,  
            LinearLayout.LayoutParams.WRAP_CONTENT); 
        mLayout.addView(contentView, params);
        Toast.makeText(LoadDocReader.this, 
            String.valueOf(mLayout.getHeight()),
            Toast.LENGTH_SHORT).show();
        
    //    scrollview.setScrollContainer(true);  
    //    scrollview.setFocusable(true);  
    //    mLayout.addView(contentView);
    //    scrollview.addView(mLayout);
        mHandler.post(mScrollView);
        
    //    int tLine = contentView.getLineCount();
    //    int tBaseLine = contentView.getBaseline();
    //    int tBottom = contentView.getBottom();
    //    Toast.makeText(LoadDocReader.this, 
    //        String.valueOf(tLine)+"*"+String.valueOf(tBaseLine)+"*"+String.valueOf(tBottom),
    //        Toast.LENGTH_SHORT).show();
       // contentView.bringPointIntoView(30);
      //  mLayout.addView(contentView, params);
      // mHandler.post(mScrollView);
      }
      
      private Runnable mScrollView = new Runnable() {  
        
        @Override  
        public void run() {  
    //       int off = mLayout.getBottom()- scrollview.getHeight();  
    //       Toast.makeText(LoadDocReader.this, String.valueOf(mLayout.getBottom())+"-----------"+String.valueOf(mLayout.getMeasuredHeight()),Toast.LENGTH_SHORT).show();
    ////       if (off > 0) {  
              scrollview.scrollTo(0, 30);//改变滚动条的位置  
    //        }  
        }    };  }
    下面是调用的地方。
    private void openFile(File f) throws IOException{
        String fileName = f.getName();
        
        String prefix=fileName.substring(fileName.lastIndexOf(".")+1);
        
        if(prefix.equals("doc"))
        {
          FileInputStream in = new FileInputStream(f);
          WordExtractor   extractor   =   new   WordExtractor(in); 
          String docStr = extractor.getText();
        //  String docSubStr = docStr.substring(0, docStr.length());
          
          Intent it = new Intent(MyFile.this, LoadDocReader.class); 
          Bundle bundle=new Bundle(); 
          bundle.putString("docStr", docStr); 
          bundle.putString("keyword", keyword);
          bundle.putInt("keywordNum", keywordNum);
          it.putExtras(bundle);
          startActivity(it);
         // finish();
        }
        else if(prefix.equals("txt"))
        {
          InputStream in = new BufferedInputStream(new FileInputStream(f));
          BufferedReader br = new BufferedReader(new InputStreamReader(in, "gb2312"));
          String txtStr = "";
          String tempTxtStr = br.readLine();
          while(tempTxtStr!=null)
          {
            txtStr = txtStr+tempTxtStr+"\n";
            tempTxtStr = br.readLine();
          }
          Intent it = new Intent(MyFile.this, LoadDocReader.class); 
          Bundle bundle=new Bundle(); 
          bundle.putString("keyword", keyword);
          bundle.putString("docStr", txtStr); 
          bundle.putInt("keywordNum", keywordNum);
          it.putExtras(bundle);
          startActivity(it);
         // finish();
          
        }
        else if(prefix.equals("pdf"))
        {
          ;
        }
    }
      

  3.   

    有这一句程序就出错了。LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(  
            LinearLayout.LayoutParams.FILL_PARENT,  
            LinearLayout.LayoutParams.WRAP_CONTENT); 
        mLayout.addView(contentView, params);
    但是下面的数据也有问题:    int tLine = contentView.getLineCount();
        int tBaseLine = contentView.getBaseline();
        int tBottom = contentView.getBottom();
        Toast.makeText(LoadDocReader.this, 
            String.valueOf(tLine)+"*"+String.valueOf(tBaseLine)+"*"+String.valueOf(tBottom),
            Toast.LENGTH_SHORT).show();tLine = 0;
    tBaseLine = -1;
    tBottom = 0;
    但是屏幕上显示的有内容。<?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
        android:id="@+id/scrollView" android:layout_width="fill_parent"  
        android:layout_height="wrap_content" android:scrollbars="vertical">
            
        <LinearLayout android:id="@+id/textLayout"  
            android:orientation="vertical" android:layout_width="fill_parent"  
            android:layout_height="wrap_content">  
            <TextView android:id="@+id/textContent" android:layout_width="fill_parent"  
                android:layout_height="wrap_content" />  
          </LinearLayout>
    </ScrollView> 
      

  4.   

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(  
            LinearLayout.LayoutParams.FILL_PARENT,  
            LinearLayout.LayoutParams.WRAP_CONTENT); 
        mLayout.addView(contentView, params);add之前先remove
      

  5.   

    没看清
    loadData里addview肯定不行,你是在oncreate里做的,而且是在已经setContentView之后又addview,只能放在setContentView前add。
      

  6.   

    TextView 里面明明显示的有数据,为什么调用它的三个函数:getLinesCount==0;getBaseline==-1;getBottom==0?
      

  7.   


    我找到答案了,我们可以这样来做,
    scrollview.post(new Runnable() {

    @Override
    public void run() {
    scrollview.scrollTo(70, 0);
    }
    });这样就不管是在oncreate里面还是其它地方都有用了,至于为什么 ,我想你一看应该能明白吧
      

  8.   

    在View为measure之前获取它的宽度和高度,肯定都是0. 这样计算出来的也是错的。
      

  9.   

    在View未measure之前获取它的宽度和高度,获取到的值是0. 这样依赖于该结果的计算也是错的。