簡體   English   中英

我如何在textview / scrollview中聚焦最后一個追加值

[英]How can i focus last append value in a textview/ scrollview

我試圖將最后一個附加值集中在文本框中。 但是不幸的是我無法提出來。 你能幫我解決這個問題嗎?

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="115dp"
android:orientation="vertical" >
<ScrollView android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="165dp"
    android:background="#FFFFFF" >

    <TextView
        android:id="@+id/tvHistory"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:gravity="right"
        android:focusable="true"
        android:text=""
        android:textColor="#000000" />
</ScrollView>

滾動視圖和追加工作正常。 但是我想用顏色集中最后附加的值

tvHistory.append(tvInput.getText()+"\n");

您不能僅使用一個TextView來完成此操作,因為單個TextView不能使用多種顏色。

您可以考慮執行以下操作:

<ScrollView android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="165dp"
    android:background="#FFFFFF" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"  >
        <TextView
            android:id="@+id/tvHistory"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#000000" />          
        <TextView
            android:id="@+id/tvHighlight"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="some_other_color" />
    </LinearLayout>
</ScrollView>

現在,每次您要附加一個新值時,請執行以下操作:

tvHistory.append(tvHighlight.getText().toString());
tvHighlight.setText(value);

在Android中,您可以使用Spannable為TextView的各個部分設置樣式。 http://developer.android.com/resources/faq/commontasks.html#selectingtext

int len = tvHistory.getText().length();
tvHistory.setText(tvHistory.getText() + tvInput.getText()+"\n", TextView.BufferType.SPANNABLE);
Spannable str = tvHistory.getText();

// Create our span sections, and assign a format to each.
str.setSpan(new BackgroundColorSpan(0xFFFFFF00), len, str.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM