Radio、CheckBoxANDToast的使用
来源:互联网 发布:淘宝店怎样提高信誉 编辑:程序博客网 时间:2024/06/06 03:03
常见的控件的使用
1. RadioButton的使用
和swing中的RadioButton很相似,一样的如果想实现单选功能的话,还是要把他们放到一个RadioGroup中。
下面看一个例子:
public class RadioActivity extends Activity {
private RadioButton radio1;
private RadioButton radio2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linear = new LinearLayout(this);
RadioGroup group = new RadioGroup(this);
radio1 = new RadioButton(this);
radio1.setText("nan");
radio2 = new RadioButton(this);
radio2.setText("nv");
group.addView(radio1);
group.addView(radio2);
linear.addView(group);
radio1.setOnCheckedChangeListener(new Radio1Listen());
group.setOnCheckedChangeListener(new RadioGroupListen());
this.addContentView(linear, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
class Radio1Listen implements OnCheckedChangeListener
{
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
{
System.out.println("change");
}
}
class RadioGroupListen implements android.widget.RadioGroup.OnCheckedChangeListener
{
public void onCheckedChanged(RadioGroup group, int checkedId)
{
if(radio1 == findViewById(checkedId))
{
System.out.println("radio1 checked");
}
else if(radio2 == findViewById(checkedId))
{
System.out.println("radio2 checked");
}
}
}
}
实现的效果如图所示:
可以实现单选的功能。
常用的事件:
(1) CompoundButton.onCheckedChangeListener当选择的控件发生改变的时候,可以添加这个事件,注意onCheckedChangeListener是CompoundButton包里面的。
这个一般会使用在CheckBox中。
(2) RadioGroup.OnCheckedChangeListener 当group中的按钮选择发生改变的时候出发的事件,注意是RadioGroup包中的。
2. CheckBox的使用
CheckBox是多选的,所以不存在组这么个说法。
看下面代码:
public class CheckBoxActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LinearLayout linear = new LinearLayout(this);
CheckBox chk1 = new CheckBox(this);
chk1.setText("love1");
CheckBox chk2 = new CheckBox(this);
chk2.setText("love2");
CheckBox chk3 = new CheckBox(this);
chk3.setText("love3");
CheckBox chk4 = new CheckBox(this);
chk4.setText("love4");
chk1.setOnCheckedChangeListener(new CheckListen());
chk2.setOnCheckedChangeListener(new CheckListen());
chk3.setOnCheckedChangeListener(new CheckListen());
chk4.setOnCheckedChangeListener(new CheckListen());
linear.addView(chk1);
linear.addView(chk2);
linear.addView(chk3);
linear.addView(chk4);
this.addContentView(linear, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
class CheckListen implements OnCheckedChangeListener
{
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
{
if(isChecked == true) {
System.out.println(buttonView.getText()+"checked");
}
else {
System.out.println(buttonView.getText()+"not checked");
}
}
}
}
当checkBox选择发生改变的时候就会出发onCheckedChangeListener这个事件。
注意:永远都是CompoundButton包中的。
3. Toast的使用
虽然他编写非常的方便,一行代码就行了。但是在开发的过程中,Toast会频繁的使用到。
MakeText方法参数说明:
makeText(Context context, CharSequence text, int duration)
第一个参数:表中当前的activity(我的理解是在哪个activity中显示)。
第二个参数:具体显示的内容。
第三个参数:显示这个数据显示多长。两个值:LENGTH_SHORT和LENGTH_LONG。
实例:
public class ToastActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LinearLayout linear = new LinearLayout(this);
Button btn = new Button(this);
btn.setText("click");
btn.setOnClickListener(new btnListen());
this.addContentView(linear, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
class btnListen implements OnClickListener
{
public void onClick(View v)
{
Toast.makeText(ToastActivity.this, "helloWorld", Toast.LENGTH_SHORT).show();
}
}
}
盲点:很多人打印的时候不显示结果,往往他们的代码是这么写的
Toast.makeText(ToastActivity.this, "helloWorld", Toast.LENGTH_SHORT);
这样实际上仅仅是获取的Toast对象。还需要调用Toast里面的show()方法.
- Radio、CheckBoxANDToast的使用
- [JavaScript] Radio的使用
- Radio Button的使用
- Radio Button的使用
- Radio Button的使用
- Radio控件的使用
- radio控件的使用
- Radio Button的使用
- Radio Button(单选框) 的使用
- Radio Button控件的使用
- input 中 radio的使用
- input 中 radio的使用 .
- MFC Radio button 的使用
- MFC Radio button 的使用
- 关于angularJS radio的使用
- jquery mobile radio的使用
- radio 使用
- .net服务器控件,radio列表的使用
- shell中的eval学习与应用
- view和activity关系
- linux path 修改 立即生效
- RelativeLayout相对布局
- (译)如何使用NSCoding和NSFileManager来保存你的应用程序数据
- Radio、CheckBoxANDToast的使用
- Linux系统中/dev/mtd与/dev/mtdblock的区别
- onLongClickListener使用
- vb6 使用其他用户启动程序
- MCS-51指令功能简述表
- dll中需要AfxGetInstanceHandle(),不能NULL
- 第一章:Linearlayout的使用
- JAVA学习之一——在DOS环境下编译遇到的问题及解决方法
- edittext实现弹出数字键盘