Cómo encontrar texto visible en la vista de texto
Soy nuevo en Android, he creado una textview y establecer el texto en esa vista de texto.
Ahora quiero encontrar sólo el texto visible en esa vista de texto. ¿Cómo puedo hacer esto?
- Motivo de la llamada a shutdown () en ExecutorService
- Android esperando el hilo de la interfaz de usuario
- Error al crear objeto Uri
- Actualizar los contenidos de recyclerview fallan en la hoja inferior
- ¿Cómo puedo utilizar el código BitmapRegionDecoder en android 2.2.2 (Froyo)?
Tratando el ejemplo de Giru Bhai:
Al intentar este código, consigo una excepción del puntero nulo.
int start = textView.getLayout().getLineStart(0); int end = textView.getLayout().getLineEnd(textView.getLineCount() - 1); String displayed = textView.getText().toString().substring(start, end);
Aquí está la salida 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
Siguiendo el código de 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); }; }
- Solución Java (framework / library / API) para aplicaciones / aplicaciones reales de crossplatform
- Diferencia entre los métodos de registro
- Convertir una cadena en int. Establezca el int en 0 si String es null
- Cómo cargar imagen a través de matriz de bytes con Glide?
- Acceda a config.properties desde AndroidManifest.xml
- Android DataPickerDialog onDateChanged no está disparando
- Convertir un teléfono Android en un joypad y conectarlo a la computadora a través de Wi-Fi
- ¿Por qué convertir un método volátil en java?
Use getLineStart()
– Devuelve el desplazamiento de texto del comienzo de la línea especificada (0 … getLineCount ()). Si la línea especificada es igual al recuento de líneas, devuelve la longitud del texto.
Y getLineEnd()
– Devuelve el desplazamiento de texto después del último carácter en la línea especificada. Para obtener el texto visible en textview como
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); } });
Esta publicación puede ayudarle también ¿Existe una manera de recuperar el recuento o rango de líneas visible de un TextView?