私は簡単なジョーク アプリケーションを持っています。ボタンを押すと、新しいジョークが表示されます。前のものが画面より大きかった場合は、下にスクロールできます。一番下に行って次のジョークに行くと、新しく生成されたジョークの一番下に移動するのですが、一番上に行って、自動的にジョークの開始を表示させたいです。これどうやってするの ?Javaコードを介して行われると思います。
お時間をいただきありがとうございます。
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);
}
}