停到某个关键字出再的地方,或者停到某一行。
解决方案 »
- 打开关闭背景数据和数据漫游
- 求解惑,小弟目前从事java开发,想转android,纠结ing~~~~~
- android 官方给出的最适合开发android的eclipse版本是哪个
- 有使用过Service的前辈吗
- android 音频问题
- 怎么解决超出屏幕的自定义view的补间动画问题?
- android平台数据包抓取
- eclipse调试 步进经常跟进到Android.jar或者DexFile.class文件怎么办?
- 关于Android拍照的一个问题
- 用安卓写一个简单的计算器
- android4.0 支持泰国语吗?
- android布局中android:layout_gravity="right"的问题
算出有多少行文本,然后找到关键词在第几行,然后根据屏幕能显示的最大行数通过scrollto的方法来手动定位滚动条的位置。
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"))
{
;
}
}
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>
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
mLayout.addView(contentView, params);add之前先remove
loadData里addview肯定不行,你是在oncreate里做的,而且是在已经setContentView之后又addview,只能放在setContentView前add。
我找到答案了,我们可以这样来做,
scrollview.post(new Runnable() {
@Override
public void run() {
scrollview.scrollTo(70, 0);
}
});这样就不管是在oncreate里面还是其它地方都有用了,至于为什么 ,我想你一看应该能明白吧