2012-06-14 15 views
5

Androidで切り捨てられたテキストを省略記号にするにはどうすればよいですか?TextViewで省略記号付きテキストを取得する方法

私はTextViewにしている:デバイス上で

<TextView 
    android:layout_width="120dp" 
    android:layout_height="wrap_content" 
    android:ellipsize="end" 
    android:singleLine="true" 
    android:text="Um longo texto aqui de exemplo" /> 

このTextViewには、次のように示されています:

"Um longo texto a..."

は、どのように私は、テキストの残りの部分を取得するのですか?

私はgetRestOfTruncate()のようなものを探しています。「qui de exemplo」を返します。

textView.getLayout()を使用
+0

私はあなたの質問のタイトルとテキストを修正しました。私はあなたに答えがあったらいいのに、これをする方法はないと思う。ここのユースケースは何ですか? –

+0

ありがとう@AustynMahoney、おそらくそれを作るために何かをしなければならない、そうでなければ、私は何かを構築するだろう、問題はそれがはるかに難しくなるだろうが、ここに投稿すれば – ademar111190

+1

'android:text =" @ string/full_text "xmlレイアウトでは、' 'getString(R.string.full_text)'が必要なときはいつでもjavaコードで記述します。 – yorkw

答えて

5
String text = (String) textView.getText().subSequence(textView.getLayout().getEllipsisStart(0), textView.getText().length()); 
+0

完璧ですが、postRunnable内にコードを挿入する必要があります。なぜなら、最初にtextviewを設計しなければならないからです。finish:新しいハンドラ()。postDelayed(コードで実行可能、1 milissegundo); – ademar111190

+2

これは 'android:singleLine =" true "'がある場合にのみ機能します。 'false'に設定し、' android:maxLines'を '以外の数に設定すると、このメソッドはテキスト全体を省略しないかのように常に返します。 –

0

アンドロイド場合getEllipsisStart(0)のみ動作します。MAXLINES設定されています:

public static String getEllipsisText(TextView textView) { 
    // test that we have a textview and it has text 
    if (textView==null || TextUtils.isEmpty(textView.getText())) return null; 
    Layout l = textView.getLayout(); 
    if (l!=null) { 
     // find the last visible position 
     int end = l.getLineEnd(textView.getMaxLines()-1); 
     // get only the text after that position 
     return textView.getText().toString().substring(end); 
    } 

    return null; 
} 
をここで単一行

を= "true" がアンドロイドがあれば動作しますソリューションです。

注意:これは、ビューが既に表示された後に機能します。

用途:

​​
関連する問題