Chinaunix首页 | 论坛 | 博客
  • 博客访问: 236707
  • 博文数量: 54
  • 博客积分: 2656
  • 博客等级: 少校
  • 技术积分: 1020
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-19 21:06
文章分类

全部博文(54)

文章存档

2016年(3)

2014年(8)

2013年(4)

2012年(2)

2011年(29)

2010年(8)

我的朋友

分类: Java

2011-02-27 22:05:15

刚开始学习android开发,做一些笔记。
button控件监听器:
 

 Button button = (Button) findViewById(R.id.button1);
 button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v)
            {
                /* 新建一个Intent对象 */
                Intent intent = new Intent();
                /* 指定intent要启动的类 */
                intent.setClass(Activity01.this, Activity02.class);
                /* 启动一个新的Activity */
                startActivity(intent);
                /* 关闭当前的Activity */
                Activity01.this.finish();
            }
 });

 

Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new ButtonListener());
    class ButtonListener implements OnClickListener{
        public void onClick(View v)
        {
            /* 新建一个Intent对象 */
            Intent intent = new Intent();
            /* 指定intent要启动的类 */
            intent.setClass(Activity01.this, Activity02.class);
            /* 启动一个新的Activity */
            startActivity(intent);
            /* 关闭当前的Activity */
            Activity01.this.finish();
        }       
   }

public class SimpleTest extends Activity implements View.OnClickListener{

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new ButtonListener());
        startBtn.setOnClickListener(this);

    }

    public void onClick(View v) {
            /* 新建一个Intent对象 */
            Intent intent = new Intent();
            /* 指定intent要启动的类 */
            intent.setClass(Activity01.this, Activity02.class);
            /* 启动一个新的Activity */
            startActivity(intent);
            /* 关闭当前的Activity */
            Activity01.this.finish();
    }

}


checkbox监听器:

 

swimBox = (CheckBox)findViewById(R.id.swim);
        runBox = (CheckBox)findViewById(R.id.run);
        readBox = (CheckBox)findViewById(R.id.read);
        swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            //该方法的第一个参数是CompoundButton对象,第二个参数是isChecked是否选中

            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub

                if (isChecked){
                    System.out.println("swimBox checked");
                }else{
                    System.out.println("swimBox unchecked");
                }
            }
        });
        
        runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub

                if (isChecked){
                    System.out.println("runBox checked");
                }else{
                    System.out.println("runBox unchecked");
                }
            }
        });
        
        readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stubrunBox

                if (isChecked){
                    System.out.println("readBox checked");
                }else{
                    System.out.println("readBox unchecked");
                }
            }
        });


swimBox = (CheckBox)findViewById(R.id.swim);
        runBox = (CheckBox)findViewById(R.id.run);
        readBox = (CheckBox)findViewById(R.id.read);
        BindCheckBoxListener(swimBox);
        BindCheckBoxListener(runBox);
        BindCheckBoxListener(readBox);
    public void BindCheckBoxListener(CheckBox checkBox){
        checkBox.setOnCheckedChangeListener(new CheckBoxListen(checkBox));
    }
    
    class CheckBoxListen implements OnCheckedChangeListener{
        public CheckBox checkBox;
        public CheckBoxListen(CheckBox checkBox){
            this.checkBox = checkBox;
        }
        
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            // TODO Auto-generated method stubrunBox

            if (isChecked){
                System.out.println(checkBox+"checked");
            }else{
                System.out.println(checkBox+"unchecked");
            }
        }        
    }



阅读(3046) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~