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]
[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
- HorizontalScrollView组合RadioGroup,实现点击RadioButton时自动滚动
- HorizontalScrollView组合RadioGroup点击RadioButton时自动滑动到屏幕的中央
- 【android】RadioGroup+RadioButton+HorizontalScrollview实现顶部横向菜单栏
- RadioGroup实现点击的RadioButton项变色
- Android实现RadioGroup中的RadioButton再次点击取消选中
- RadioGroup 和 RadioButton 点击事件
- RadioGroup自动添加RadioButton实现屏幕适配
- HorizontalScrollView 实现水平滚动
- RadioGroup中的RadioButton实现换行
- 自定义RadioGroup实现radioButton多行多列
- RadioGroup和RadioButton的实现
- FrameLayout+RadioGroup、RadioButton 点击按钮跳转页面
- RadioGroup中点击切换RadioButton背景
- 自定义导航栏HorizontalScrollView+RadioGroup实现
- RadioGroup和Fragment相结合实现点击RadioButton进行Fragment的切换
- RadioButton和HorizontalScrollView实现分类标签
- 利用HorizontalScrollView实现水平滚动
- HorizontalScrollView 初始化滚动的实现
- 【转载】XCode工程中 Project 和 Targets区别
- 机器学习中常用到的知识点总结
- php的pdo数据层mysql类
- HDU 1671 Phone List 字典树(Trie)
- 3.Redis keys 命令
- HorizontalScrollView组合RadioGroup,实现点击RadioButton时自动滚动
- QPainter的用法
- JAVA HashMap详解
- 简单谈谈Cookie,Session与Cache之间的区别
- A - Tree Recovery(已知前序中序 求后序)
- xcodebuild脚本打包的使用
- 分析DHCP Log,对比出未加域的黑名单
- HDU 2089 不要62 && HDU 3555 Bomb (数位DP)
- 图论 BFS POJ 3126