Activity常用控件

来源:互联网 发布:3m互助系统源码 编辑:程序博客网 时间:2024/05/21 09:53

一、种类

1. TextView 文本控件

2. Button 按钮控件

3. RadioGroup  单选按钮组控件

    RadioButon  单选控件

4. CheckBox 复选框控件

5. Toast控件

6. ProgressBar控件

二、布局文件中添加控件

<widget

   android:id="@+id/name"

   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/content"></widget>

对于ProgressBar控件还有选项:

  style="?android:attr/progressBarStyleHorizontal" //进度条的风格

  android:visibility="gone"//进度条初始不显示

三、Activity中获取控件id

       Widget  widget = (Widget)findViewById(R.id.name);

四、设置监听器

        ①Button

            button1.setOnClickListener(new MyButtonListener());  //设置监听事件

            class MyButtonListener implements OnClickListener{
             @Override
              public void onClick(View v) { //单击时的动作
            // TODO Auto-generated method stub
              }
          }

      ②RadioGroup

          radioGroup.setOnCheckedChangeListener(new myRadioGroup());

          class myRadioGroup implements RadioGroup.OnCheckedChangeListener{
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub
            if (radioButton1.getId() == checkedId)
            {
                Toast.makeText(MainActivity.this, "male", Toast.LENGTH_SHORT).show();
            }else if (radioButton2.getId() == checkedId)
            {
                Toast.makeText(MainActivity.this, "female", Toast.LENGTH_SHORT).show();
            }
          }       
       }

   ③CheckBox

      checkBox1.setOnCheckedChangeListener(new myCheckBox1());

      class myCheckBox1 implements CompoundButton.OnCheckedChangeListener{
        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            // TODO Auto-generated method stub
            if (isChecked)
            {
                Toast.makeText(MainActivity.this, "swim", Toast.LENGTH_SHORT).show();
            }
        }    
    }

五 控件常用函数

Toast:

Toast.makeText(MainActivity.this, "swim", Toast.LENGTH_SHORT).show();

ProgressBar:

firstBar.setVisibility(View.VISIBLE); //显示
firstBar.setMax(150);//设置进度条最大值

firstBar.setProgress(i);//设置进度条第一进度

firstBar.setSecondaryProgress(i+10);//设置进度条第二进度

0 0