gpt4 book ai didi

java - 如何在 TextView 中查找可见文本

转载 作者:太空宇宙 更新时间:2023-11-03 10:20:15 25 4
gpt4 key购买 nike

我是 android 新手,我创建了一个 TextView 并在该 TextView 上设置了一些文本。

现在我只想找到该 TextView 上的可见文本。我该怎么做?

尝试 Giru Bhai 的例子:

尝试这段代码时,出现空指针异常。

int start = textView.getLayout().getLineStart(0);
int end = textView.getLayout().getLineEnd(textView.getLineCount() - 1);
String displayed = textView.getText().toString().substring(start, end);

这是 LogCat 输出:

06-28 18:53:04.142: E/AndroidRuntime(28521): Process: com.example.simple, PID: 28521
06-28 18:53:04.142: E/AndroidRuntime(28521): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simple/com.example.simple.VendorDetailsActivity}: java.lang.NullPointerException
06-28 18:53:04.142: E/AndroidRuntime(28521): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
06-28 18:53:04.142: E/AndroidRuntime(28521): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
06-28 18:53:04.142: E/AndroidRuntime(28521): at android.app.ActivityThread.access$800(ActivityThread.java:139)
06-28 18:53:04.142: E/AndroidRuntime(28521): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
06-28 18:53:04.142: E/AndroidRuntime(28521): at android.os.Handler.dispatchMessage(Handler.java:102)
06-28 18:53:04.142: E/AndroidRuntime(28521): at android.os.Looper.loop(Looper.java:136)
06-28 18:53:04.142: E/AndroidRuntime(28521): at android.app.ActivityThread.main(ActivityThread.java:5102)
06-28 18:53:04.142: E/AndroidRuntime(28521): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 18:53:04.142: E/AndroidRuntime(28521): at java.lang.reflect.Method.invoke(Method.java:515)
06-28 18:53:04.142: E/AndroidRuntime(28521): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-28 18:53:04.142: E/AndroidRuntime(28521): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-28 18:53:04.142: E/AndroidRuntime(28521): at dalvik.system.NativeStart.main(Native Method)
06-28 18:53:04.142: E/AndroidRuntime(28521): Caused by: java.lang.NullPointerException
06-28 18:53:04.142: E/AndroidRuntime(28521): at com.example.simple.VendorDetailsActivity.onStart(VendorDetailsActivity.java:94)
06-28 18:53:04.142: E/AndroidRuntime(28521): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1194)
06-28 18:53:04.142: E/AndroidRuntime(28521): at android.app.Activity.performStart(Activity.java:5258)
06-28 18:53:04.142: E/AndroidRuntime(28521): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2182)
06-28 18:53:04.142: E/AndroidRuntime(28521): ... 11 more

VendorDetailsActivity.java 代码如下:

import java.util.ArrayList;
import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.TouchDelegate;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class VendorDetailsActivity extends Activity {
private ArrayList<Vendor> arrayVendor;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_vendor_details);
Bundle bundle = getIntent().getExtras();
String json = bundle.getString("json_data");
String vName = bundle.getString("vendorName");

final Button backButton = (Button) findViewById(R.id.vendordetailsbackbutton);
final View parent = (View) backButton.getParent();
parent.post(new Runnable() {

public void run() {
final Rect r = new Rect();
backButton.getHitRect(r);
r.top -= 20;
r.bottom += 20;
r.right += 70;
parent.setTouchDelegate(new TouchDelegate(r, backButton));
}
});
VendorActivity vendor = new VendorActivity();
arrayVendor = vendor.getMessage(json);
String[] vendorName = new String[arrayVendor.size()];
String[] vendorBooth = new String[arrayVendor.size()];
String[] vendorBio = new String[arrayVendor.size()];
String[] vendorPhoto = new String[arrayVendor.size()];
backButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});

for (int i = 0; i < arrayVendor.size(); i++) {

vendorName[i] = arrayVendor.get(i).vName;
vBio[i] = arrayVendor.get(i).vBio;
vendorPhoto[i] = arrayVendor.get(i).vLogo;
vendorBooth[i] = arrayVendor.get(i).vBooth;
if (vName.equals(vendorName[i]) || vName.equals(vendorBooth[i])) {
new DownloadImage((ImageView) findViewById(R.id.vendor_photo))
.execute(vendorPhoto[i]);


TextView vendorBoothhash = (TextView) findViewById(R.id.booth_hash);
vendorBoothhash.setText(vendorBooth[i]);
TextView vendorbio = (TextView) findViewById(R.id.vendor_bio);
vendorbio.setText(vendorBio[i]);


TextView vendorname = (TextView) findViewById(R.id.vendor_name);
vendorname.setText(vendorName[i]);

}
}
}

@Override
protected void onStart() {
TextView textView = (TextView) findViewById(R.id.vendor_bio);
int start = textView.getLayout().getLineStart(0);
int end = textView.getLayout().getLineEnd(textView.getLineCount() - 1);
String displayed = textView.getText().toString().substring(start, end);
TextView vendorbio1 = (TextView) findViewById(R.id.vendor_bio1);
vendorbio1.setText(displayed);
System.out.println(displayed);
};

}

最佳答案

使用 getLineStart() - 返回指定行开头的文本偏移量 (0…getLineCount())。如果指定行等于行数,则返回文本的长度。

getLineEnd() - 返回指定行最后一个字符后的文本偏移量。将 TextView 中的可见文本作为

 ViewTreeObserver vto = txtCommName.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int start = textView.getLayout().getLineStart(0);
int end = textView.getLayout().getLineEnd(textView.getLineCount() - 1);
String displayed = textView.getText().toString().substring(start, end);
TextView vendorbio1 = (TextView) findViewById(R.id.vendor_bio1);
vendorbio1.setText(displayed);
}
});

这篇文章也可能对您有所帮助Is there a way of retrieving a TextView's visible line count or range?

关于java - 如何在 TextView 中查找可见文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24466801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com