1. 程式人生 > >HorizontalScrollView組合RadioGroup,實現點選RadioButton時自動滾動

HorizontalScrollView組合RadioGroup,實現點選RadioButton時自動滾動

做開發時用到這個小應用:點選RadioButton時自動滑動到螢幕的中央,java程式碼如下
[mw_shl_code=java,true]
public class ScrollTextActivity extends Activity {
        HorizontalScrollView scrollView;
        RadioGroup radio_group;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                setContentView(R.layout.main_activity);

                scrollView = (HorizontalScrollView) findViewById(R.id.scrollview);
                radio_group = (RadioGroup) findViewById(R.id.radio_group);


                //幕
                Display d = getWindowManager().getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();

        d.getMetrics(dm);
        final int screenHalf = d.getWidth()/2;//螢幕寬度的一半
                radio_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){

                        @Override
                        public void onCheckedChanged(RadioGroup group, int checkedId) {

                                int scrollX = scrollView.getScrollX();

                                System.out.println("scrollX----->"+scrollX);
                                RadioButton rb = (RadioButton) findViewById(checkedId);
                                int left = rb.getLeft();
                                int leftScreen = left-scrollX;
                                scrollView.smoothScrollBy((leftScreen-screenHalf), 0);
                        }});
        }
}[/mw_shl_code]

佈局檔案main_activity.xml
[mw_shl_code=html,true]<?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" >
    <HorizontalScrollView
        android:id="@+id/scrollview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <RadioGroup
            android:gravity="center_vertical"
            android:id="@+id/radio_group"
            android:layout_width="fill_parent"
            android:layout_height="80dip"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/button1"
                android:text="@string/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <RadioButton
                android:layout_marginLeft="30dip"
                android:id="@+id/button2"
                android:text="@string/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <RadioButton
                android:layout_marginLeft="30dip"
                android:id="@+id/button3"
                android:text="@string/button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <RadioButton
                android:layout_marginLeft="30dip"
                android:id="@+id/button4"
                android:text="@string/button4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <RadioButton
                android:layout_marginLeft="30dip"
                android:id="@+id/button5"
                android:text="@string/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </RadioGroup>
    </HorizontalScrollView>
</LinearLayout>[/mw_shl_code]