2012-05-08 7 views
1

私は簡単なジョークアプリケーションを持っています。ボタンを押すと新しいジョークが得られます。前のものが画面より大きかった場合は、下にスクロールすることができます。あなたが一番下に下がって次の冗談に行くと、あなたは新しく生成された冗談の一番下に移動しますが、それを一番上に移動して冗談の開始を自動的に表示します。これどうやってするの ? 私はそれがJavaコードを介して行われると仮定します。textViewで新しく生成されたテキストの先頭にどのようにフォーカスするのですか?

ありがとうございます。

+0

それを何ですかコンポーネント、およびWHあなたはxmlをタグに入れましたか? – Tharwen

答えて

0

scrollTo(int x, int y)メソッドを使用して、私はTextViewの周りにScrollViewを持っているのが好きですが、TextViewだけでも同じことができると思います。あなたが理解して欲しい!

ロルフ

のxml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ScrollView 
     android:id="@+id/scroll" 
     android:layout_width="fill_parent" 
     android:layout_height="130dp" > 

     <TextView 
      android:id="@+id/text" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="long\n\n\n\n\n\n\n\n long\n\n\n\n\n\n\n very text here!" /> 

    </ScrollView> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="joke" /> 

</LinearLayout> 

のjava:

package org.sample.example; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ScrollView; 
import android.widget.TextView; 

public class AutoscrollActivity extends Activity implements OnClickListener { 
    /** Called when the activity is first created. */ 

    private Button new_joke; 
    private TextView joke; 
    private ScrollView scroll; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     new_joke = (Button) this.findViewById(R.id.button); 
     new_joke.setOnClickListener(this); 
     joke = (TextView) this.findViewById(R.id.text); 
     scroll = (ScrollView) this.findViewById(R.id.scroll); 
    } 

    @Override 
    public void onClick(View v) { 
     joke.setText("Long\n\n\n\n\n\n\n\n joke \n\n\n\n\n\n\n\nlong joke joke"); 
     scroll.scrollTo(0, 0); 
    } 
} 
+0

これはまさに私が探していたものだと思います。 –

関連する問題