1. 程式人生 > >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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

通過匿名內部類的方式

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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

讓當前類實現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;
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

申明一個方法,方法名和你要點選的按鈕的方法相同,在activity內定義一個方法.(適合快速demo)

layout:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="撥打"
    android:onClick="clickbutton"
  />

Activity:

public void clickbutton(View view){
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

RadioButton點選事件

RadioButton佈局

RadioButton嵌在一個RadioGroup中

<RadioGroup
            android:id="@+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">

            <RadioButton
                android:id="@+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" />

          <!--android:checked="true" 預設選中
              android:button="@null"去掉radiobutotn的選擇框
              android:drawableTop="@drawable/selected_ic_home" 圖片位於文字下方
              android:textColor="@drawable/selected_rg_rb_text" 文字的背景,也有背景選擇,添加了選中改變背景的功能  
           -->
            <RadioButton
                android:id="@+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" />

             <RadioButton
                android:id="@+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" />

            <RadioButton
                android:id="@+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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67

使用預設佈局時顯示的樣式,顯示的是奪寶這個樣式 
預設佈局樣式

使用 < 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;
                }
            }
        });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

背景選擇器

RadioButton 的選擇器檔案

<!-- selected_rg_rb_text.xml 文字選中改變成紅色-->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="@color/color_red7"/>
    <!-- not selected -->
    <item android:state_checked="false" android:color="#878787"/>
</selector>


----------
<!--selected_ic_find.xml  圖片改變背景,其實是兩種圖片分別在點選和不點選顯示-->
<?xml version="1.0" encoding="utf-8"?>
<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

RadioButton的實現效果

RadioButton的點選效果 
可以用來改變ListView和Button控制元件的預設背景

    <?xml version="1.0" encoding="utf-8" ?>     
    <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>  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

Button的選擇器可以更復雜,圓角,邊框顏色大小,陰影,都可設定

<?xml version="1.0" encoding="utf-8"?>
<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

注意: 
ListVIew使用時設定背景使其透明,防止顯示錯誤 
Android:cacheColorHint="@android:color/transparent" 
Button設定獲取焦點 
android:focusable="true" 
android:backgroud="@drawable/button_color"

相關推薦

RadioButton變色

Button的點選事件 通過內部類方式 button = (Button) findViewById(R.id.button); button.setOnClickListener(new MyClickListener()); } private cl

Vue.js指令小練習001 列表變色

需求: 分析: 給li繫結一個背景色樣式,當你點選這個li的時候,這個li背景色的樣式生效,其他的li背景色樣式不生效 程式碼: <!DOCTYPE html> <html> <head> <meta charset="UTF-8

超連結按鈕變色,原來的連結恢復原色

視訊播放列表的超連結按鈕點選變色,原來的恢復原色,主要是讓使用者清楚的知道此刻播放的是哪一集。這裡所說的實現方法並不是利用css的link、hover、active所實現的,雖然css也能做到超連結點選變色,但是卻不能用到這裡的需求上,因為css實現的點選變色,是不能再點選頁面其他地方的,很顯然,看視訊的時候

vue變色

<template> <div> <router-link to="/w1">點選回去</router-link> <div v-for="(x,index) in list" v-bind:class="{red

jQuery 實現相同控制元件變色

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html

變色

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>松樹&l

RadioGroup radiobutton onCheckedChanged()呼叫兩次或多次 問題

專案中 首頁 RadioGroup控制元件,radiobutton點選切換的時候,onCheckedChanged方法被執行了兩次,困擾了大半天。剛開始以為是頁面呼叫不當,被建立了兩次,可以用單例去解決

AngularJS新增樣式、變色設定

首先解釋需求是這樣的,有個列表,當你點選哪一行時,哪一行背景變成灰色,在JQ中,大家都知道,這是非常容易的,加一個addClass就行了,那麼AngularJS如何實現呢? 下面我們看程式碼部分 <!doctype html> <h

RadioButton事件不起作用的坑

最近做開發用到了RadioButton, 並自定義了樣式,把RadioButton的按鈕去掉了,換成了自定義的點選效果,這時候點選看不見是否被選中的效果。於是問題來了,第一次點選沒問題,第二次以後點選就失效了。 佈局是這樣的: <RadioButton andro

Androidstudio之TextView變色

使用環境:  1 文字資訊點選變色            1.1 點選變色,再點選恢復顏色          1.2 點選變色,鬆開恢復 1.1 處理方法: <TextView android:id="@+id/textWorker" android:layout

有關RADIOBUTTON延遲

try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printS

android textview變色 鬆開恢復或不恢復

   一、textview 點選變色,鬆開恢復。效果如左圖:    二、鬆開後不恢復。如右圖:                                                                一、textview 點選變色,鬆開恢復

【日常新手入門】android之變色

實現點選button改變button的顏色只需要在drawable中新增selector.xml檔案即可<selector xmlns:android="http://schemas.android.com/apk/res/android">           

小程式 列表變色

工作日誌 隨手筆記 僅供參考通過點選時候判斷點選的是哪一個,然後判斷item ==shopitem 使之變色<view class='shop_list' > <view class="shop_item{{item == shopitem?

在Android studio中設定按鈕變色效果的方法

一、在drawable下新建一個xml 名字"test" 程式碼如下: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.androi

iOS UICollectionView 按鈕變色(收藏贊功能)實現

1.前言 專案需求要實現點選收藏功能,但是頁面資料進行了分頁功能,當載入了第二頁資料後,收藏按鈕的顯示就紊亂,具體原因是點選收藏後,請求收藏介面成功後要對資料進行重新整理,這個時候因為分頁的原因,載入過來的資料只是第二頁的(或者第一頁,反正只有一頁),這

unity 滑鼠拖動 物體旋轉 變色 拖動移動

public Camera camera; Ray ray; RaycastHit hitInfo; Vector3 offset; GameObject obj;//獲取點選到的物體 public GameObject tr

ListView的變色

   我做了一個音樂播放器,其中用ListView顯示歌曲列表,當我點選某一首歌時,希望這首歌所在的item的背景顏色改變,以突出顯示所選擇的歌曲。    首先我想到的是在ItemClickListener裡面寫,如下: private View forme

ImageButton加shape和selector實現簡單易懂的變色

在實際專案中經常會用到按鈕點選變色的效果,有很多種辦法,最近看到了一種新的寫法,比較簡單好理解 上程式碼! main.xml <RelativeLayout xmlns:android="ht

【前端】angularJS或者jquery實現按鈕變色,再變回來

目的:使按鈕在兩種狀態之間切換,注意:簡單的通過.修改focus和active的樣式是做不到的。 效果圖: html程式碼: <div class="col-1">