单击TextView1以跳转到ScrollView中的TextView2

时间:2012-09-17 19:40:30

标签: android text scroll textview

我在ScrollView中点击了“TextView1”,我需要让屏幕在文本中低到“TextView2”文本。

1 个答案:

答案 0 :(得分:0)

这是我的布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main_scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textViewTitle"
        android:onClick="doClick"
        android:clickable="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/padding_medium"
        android:layout_marginBottom="1500dp"
        android:text="@string/hello_world" />

    <TextView
        android:id="@+id/textViewText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/padding_medium"
        android:text="sdfasiufhadifhissipaghuopghshgiahipuhguiashguipahguiphiughsdupaghapsghasuipghasughpgh" />

</LinearLayout>

这是我的代码:

public class MainActivity extends Activity {

private ScrollView scrollView;
private TextView textViewAim;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        scrollView =  (ScrollView) findViewById(R.id.activity_main_scrollview);
        textViewAim = (TextView)   findViewById(R.id.textViewText);
    }

    public void doClick(View view){
        if (view.getId() == R.id.textViewTitle) {
            int[] location = new int[2];
            textViewAim.getLocationOnScreen(location);
            scrollView.scrollTo(location[0], location[1]);
        }
    }
}

享受!