HorizontalScrollView组合RadioGroup,实现点击RadioButton时自动滚动

来源:互联网 发布:炒股软件接口 编辑:程序博客网 时间:2024/04/30 13:59
做开发时用到这个小应用:点击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]
0 0