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");

           }

       }

       

    }

}

实现的效果如图所示:

Radio、CheckBoxANDToast的使用 - Paul - Its my life
 

可以实现单选的功能。

常用的事件:

(1)     CompoundButton.onCheckedChangeListener当选择的控件发生改变的时候,可以添加这个事件,注意onCheckedChangeListenerCompoundButton包里面的。

这个一般会使用在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()方法.

原创粉丝点击