老罗视频学习笔记。
一.CheckBox控件,动态加载布局文件。
首先创建一个checkboxseekbar.xml文件,内容如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/checkboxButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBoxButton" />
</LinearLayout>
再创建一个checkbox.xml文件,内容是CheckBox,如下:
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="@+id/checkbox"
android:layout_height="wrap_content" >
</CheckBox>
在程序代码中动态加载:
//定义一个ArrayList,里边数据是CheckBox类对象
private List<CheckBox> checkBoxs = new ArrayList<CheckBox>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//先设置下要填入CheckBox的内容
String[] checkboxText = new String[]{"你是学生吗?","你喜欢android吗?","你喜欢旅游吗?","打算出国吗?"};
//动态加载布局
LinearLayout linearLayout = (LinearLayout)getLayoutInflater().inflate(R.layout.checkboxseedbar, null);
//给指定的checkbox赋值
for(int i =0;i<checkboxText.length;i++){
//先获得checkbox.xml的对象
CheckBox checkBox = (CheckBox)getLayoutInflater().inflate(R.layout.checkbox,null);
checkBoxs.add(checkBox);
checkBoxs.get(i).setText(checkboxText[i]);
//在checkboxseedbar.xml布局中加载checkbox布局
linearLayout.addView(checkBox,i);
}
setContentView(linearLayout);
Button button = (Button)findViewById(R.id.checkboxButton);
button.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String s = "";
for(CheckBox checkBox:checkBoxs){
if(checkBox.isChecked()){
s += checkBox.getText() + "\n";
}
}
if("".equals(s)){
s = "你还没有选中选项";
}
//使用一个提示框来提示用户信息
new AlertDialog.Builder(this).setMessage(s).setPositiveButton("关闭", null).show();
}
效果如下:
二.SeekBar控件的使用。
布局还在checkboxseekbar.xml文件里写,和CheckBox例子的布局用LinearLayout隔开。
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/textviewSeek1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/textviewSeek2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TextView>
<SeekBar android:id="@+id/SeekBar1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="30"/>
<SeekBar android:id="@+id/SeekBar2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="30"
android:secondaryProgress="90"/>
</LinearLayout>
android:progress是当前滑块的位置。
android:secondaryProgress是第二滑块的位置,一般用于视频下载的进度条。
让这个类再继承一下OnSeekBarChangeListener
public class CheckBoxSeedBar extends Activity implements OnClickListener,OnSeekBarChangeListener{
有三个必备的重载函数。
//OnSeekBarChangeListener监控seekbar上的移动事件
//滑动滑竿触发的事件
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean arg2) {
// TODO Auto-generated method stub
if(seekBar.getId() == R.id.SeekBar1){
textViewSeek1.setText("seekBar1的当前位置是:"+progress);
}else {
textViewSeek2.setText("seekBar2的当前位置是:"+progress);
}
}
//表示从哪里开始拖动
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
if (seekBar.getId()==R.id.SeekBar1) {
textViewSeek1.setText("seekBar1开始拖动");
} else {
textViewSeek2.setText("seekBar2开始拖动");
}
}
//表示从哪里结束拖动
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
if (seekBar.getId()==R.id.SeekBar1) {
textViewSeek1.setText("seekBar1停止拖动");
} else {
textViewSeek2.setText("seekBar2停止拖动");
}
}
效果图如下: