Android Button的四种点击事件及RadioButton 的点击事件,背景选择器的使用


山有木兮木有枝,心悦君兮君不知。 —–佚名《越人歌》


  • Button的点击事件
    • 通过内部类方式
    • 通过匿名内部类的方式
    • 让当前类实现OnClickListener接口类型布局有很多按钮时使用
    • 申明一个方法方法名和你要点击的按钮的方法相同在activity内定义一个方法适合快速demo
  • RadioButton点击事件
    • RadioButton布局
    • RadioButton的点击事件代码
    • 背景选择器
      • RadioButton的实现效果

Button的点击事件

通过内部类方式

button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new MyClickListener());

}
private  class MyClickListener implements View.OnClickListener{
    @Override
    public void onClick(View view) {
        //去输入字符的空格
        String number = etc_number.getText().toString().trim();
        if("".equals(number)){
            Toast.makeText(MainActivity.this,"number不能为空",Toast.LENGTH_LONG).show();
            return;
        }
        //创建意图对象
        Intent intent = new Intent();
        //设置动作
        intent.setAction(Intent.ACTION_CALL);
        //设置要拨打的数据
        /**
         * url:统一资源定位符www.baidu.com
         * uri:统一资源标识符,自己定义的路径,想代表什么都可以
         */
        intent.setData(Uri.parse("tel:"+number));
        //开启意图
        startActivity(intent);

通过匿名内部类的方式

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        String number = etc_number.getText().toString().trim();
        if("".equals(number)){
            Toast.makeText(MainActivity.this,"number不能为空",Toast.LENGTH_LONG).show();
            return;
        }
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:"+number));
        startActivity(intent);

让当前类实现OnClickListener接口类型(布局有很多按钮时使用)

public class MainActivity2 extends AppCompatActivity implements View.OnClickListener {
    private EditText etc_number;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        etc_number = (EditText) findViewById(R.id.editText);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);
        //当有多个按钮时,判断
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button1:
                callphone();
                break;
            case R.id.button2:
                callphone();
                break;
            case R.id.button3:
                callphone();
                break;
            case R.id.button4:
                callphone();
                break;
        }
    }

申明一个方法,方法名和你要点击的按钮的方法相同,在activity内定义一个方法.(适合快速demo)

layout:


RadioButton点击事件

RadioButton布局

RadioButton嵌在一个RadioGroup中

"@+id/main_rg"
            android:background="@color/color_white"
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:orientation="horizontal"
            android:paddingBottom="5dp"
            android:paddingTop="4dp">

            "@+id/main_db"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:checked="true"
                android:drawableTop="@drawable/selected_ic_home"
                android:gravity="center_horizontal|bottom"
                android:text="夺宝"
                android:textColor="@drawable/selected_rg_rb_text"
                android:textSize="12sp" />

          
            "@+id/main_fx"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:checked="false"
                android:drawableTop="@drawable/selected_ic_find"
                android:gravity="center_horizontal|bottom"
                android:text="发现"
                android:textColor="@drawable/selected_rg_rb_text"
                android:textSize="12sp" />

             "@+id/main_qd"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:checked="false"
                android:drawableTop="@drawable/selected_ic_listing"
                android:gravity="center_horizontal|bottom"
                android:text="清单"
                android:textColor="@drawable/selected_rg_rb_text"
                android:textSize="12sp" />

            "@+id/main_me"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:checked="false"
                android:drawableTop="@drawable/selected_ic_user"
                android:gravity="center_horizontal|bottom"
                android:text="我的"
                android:textColor="@drawable/selected_rg_rb_text"
                android:textSize="12sp" />

        </RadioGroup>

使用默认布局时显示的样式,显示的是夺宝这个样式
默认布局样式

使用 < selector>背景选择器后,显示的样式
这里写图片描述


RadioButton的点击事件代码

    private RadioGroup mainRg;
    private RadioButton mainDb;
    private RadioButton mainFx;
    private RadioButton mainQd;
    private RadioButton mainMe;
    ....
    //底部四个按钮 夺宝 发现 清单 我的
        mainDb = (RadioButton) findViewById(R.id.main_db);
        mainFx = (RadioButton) findViewById(R.id.main_fx);
        mainQd = (RadioButton) findViewById(R.id.main_qd);
        mainMe = (RadioButton) findViewById(R.id.main_me);
    //RadioGroup的点击事件 
        mainRg = (RadioGroup) findViewById(R.id.main_rg);
        mainRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {

                switch (checkedId) {
                    case R.id.main_db:
                       //点击执行逻辑
                        break;
                    case R.id.main_fx:

                        break;
                    case R.id.main_qd:

                        break;
                    case R.id.main_me:

                    default:
                        break;
                }
            }
        });

背景选择器

RadioButton 的选择器文件



<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="@color/color_red7"/>
    
    <item android:state_checked="false" android:color="#878787"/>
selector>


----------


<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_find_selected" android:state_checked="true"/>
    <item android:drawable="@drawable/ic_find" android:state_checked="false" />
selector>

RadioButton的实现效果

RadioButton的点击效果
可以用来改变ListView和Button控件的默认背景

         
    <selector xmlns:android="http://schemas.android.com/apk/res/android">   
        
      <item android:drawable="@drawable/pic1" />      
        
      <item android:state_window_focused="false"     
            android:drawable="@drawable/pic1" />     
        
      <item android:state_focused="true" android:state_pressed="true"   android:drawable= "@drawable/pic2" />   
        
    <item android:state_focused="false" android:state_pressed="true"   android:drawable="@drawable/pic3" />    
        
      <item android:state_selected="true"   android:drawable="@drawable/pic4" />     
        
      <item android:state_focused="true"   android:drawable="@drawable/pic5" />     
    selector>  

Button的选择器可以更复杂,圆角,边框颜色大小,阴影,都可设置


<selector xmlns:android="http://schemas.android.com/apk/res/android">                 /
        <item android:state_pressed="true">//定义当button 处于pressed 状态时的形态。
                <shape>
                        <gradient android:startColor="#8600ff" />
                        <stroke     android:width="2dp" android:color="#000000" />
                        <corners android:radius="5dp" />
                        <padding android:left="10dp" android:top="10dp"
                                         android:bottom="10dp" android:right="10dp"/>
                shape>
        item>
        <item android:state_focused="true">//定义当button获得 focus时的形态
                <shape>
                        <gradient android:startColor="#eac100"/>
                        <stroke     android:width="2dp" android:color="#333333"    color="#ffffff"/>
                        <corners android:radius="8dp" />
                        <padding android:left="10dp" android:top="10dp"
                                         android:bottom="10dp" android:right="10dp"/>
                shape>
        item>
selector>

注意:
ListVIew使用时设置背景使其透明,防止显示错误
android:cacheColorHint="@android:color/transparent"
Button设置获取焦点
android:focusable="true"
android:backgroud="@drawable/button_color"

你可能感兴趣的:(Android笔记)