2017-08-26 3 views
2

私は電卓アプリをやっています。TextViewの最後の数字に水平スクロールとフォーカス

私はユーザーが詳細を示すために数値を書き込むTextViewを持っています。

TextViewはHorizo​​ntalScrollViewの内部にあります。 Horizo​​ntalScrollViewは、垂直のLinearLayoutの内側にあります。 TextViewは1行で、ユーザーが長い番号を書き込むと、番号が画面から出てきます。

TextViewは、ユーザーが追加した最後の番号の後に表示されるため、画面をスクロールせずに追加された最後の番号を表示できます。私は毎秒スクロールを繰り返すオートスクロール機能を望んでいません。

この

はTextViewに

<HorizontalScrollView 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 

      <TextView 
       android:id="@+id/numberDisplay" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="0dp" 
       android:layout_marginLeft="8dp" 
       android:layout_marginRight="8dp" 
       android:layout_marginTop="16dp" 
       android:hint="0" 
       android:scrollHorizontally="true" 
       android:maxLines="1" 
       android:layout_gravity="right" 
       android:textSize="70sp" /> 

     </HorizontalScrollView> 

答えて

1

あるこのお試しください:

TextView textView; 
HorizontalScrollView scrollView; 
  • onCreate()でそれらを初期化します。あなたの活動で

    1. は、あなたのスクロールビューとテキストビューのための変数を作成します:

      scrollView = (HorizontalScrollView) findViewById(R.id.scrollView); // Add this id to xml 
      textView = (TextView) findViewById(R.id.numberDisplay); 
      
    2. あなたは、スクロールビューのテキストビュー更新スクロール位置のテキストを更新するたびに:

      scrollView.scrollTo(textView.getRight(), textView.getTop()); 
      

    アップデート:私は実現 つまり、あなたはすぐにそれをscrollView.scrollTo(...)textView.setText(...)を呼び出している場合テキストビューのサイズがすぐに更新されないため、機能しません。あなたはscollの位置を次のように更新する必要があります:

    scrollView.post(new Runnable() { 
         @Override 
         public void run() { 
          scrollView.scrollTo(textView.getRight(), textView.getTop()); 
         } 
        });