如何使用数据绑定在checkChanged事件上绑定RadioGroup上的方法

时间:2016-09-10 14:45:37

标签: android android-databinding android-radiogroup

我在互联网上搜索如何通过RadioGroup执行新的酷数据绑定,我没有找到关于它的单一博文。

这是一个简单的场景,基于所选的单选按钮,我想使用android数据绑定附加一个回调事件。我没有在xml部分找到允许我定义回调的任何方法。

就像这里是我的RadioGroup:

      <RadioGroup
            android:id="@+id/split_type_radio"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:checkedButton="@+id/split_type_equal"
            android:gravity="center"
            <!-- which tag ? -->
            android:orientation="horizontal">

           ...

       </RadioGroup>

如何附加将在RadioGroup checkChnged事件上调用的处理程序方法将使用数据绑定触发?

我尝试在布局文件中使用onClick(不知道它是否相同)并在Activity中定义方法并在布局文件中使用它来定位它:

   <variable
        name="handler"
        type="com.example.MainActivity"/>

  ...
   <RadioGroup
        android:onClick="handler.onCustomCheckChanged"
        .. >

定义方法onCustomCheckChanged是这样的:

public void onCustomCheckChanged(RadioGroup radio, int id) {
     // ...
}

但是,它给了我编译错误:

  

错误:(58,36)侦听器类android.view.View.OnClickListener与方法onClick不匹配任何方法handler.onCustomCheckChanged

的签名

我看过很多博客都提到RadioGroup是可能的,但是他们中没有人真正说过如何。如何处理data-binding

5 个答案:

答案 0 :(得分:11)

在深入研究这些方法之后,我在SO上找到了this问题,这有助于我理解如何绑定单个侦听器方法。

以下是关于RadioGroup的事情:

RadioGroup监听器中,您有一个方法onCheckedChanged(RadioGroup g, int id)。因此,您可以通过将其实例作为布局文件中的变量传递并调用具有相同签名的方法,将该方法直接绑定到您的处理程序或您的活动。

因此请调用布局文件:

  <RadioGroup
        android:id="@+id/split_type_radio"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:checkedButton="@+id/split_type_equal"
        android:gravity="center"
        android:onCheckedChanged="@{handler.onSplitTypeChanged}"
        android:orientation="horizontal">

       ...

   </RadioGroup>

在我的活动或处理程序中,我需要简单地为方法提供相同的名称和签名:

public void onSplitTypeChanged(RadioGroup radioGroup,int id) {
  // ...
}

确保方法公开。

注意:这适用于任何(大多数,我没有尝试过)侦听器方法。与EditText一样,您可以提供android:onTextChanged等等。

答案 1 :(得分:10)

我正在使用字符串,在这种情况下,我可以基于viewModel.getCommuteType() viewModel.setCommuteType(String)

进行绑定
<RadioGroup
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<RadioButton
    android:checked="@{viewModel.commuteType.equals(Commute.DRIVING)}"
    android:onClick="@{()->viewModel.setCommuteType(Commute.DRIVING)}"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="D"/>

<RadioButton
    android:checked="@{viewModel.commuteType.equals(Commute.BICYCLE)}"
    android:onClick="@{()->viewModel.setCommuteType(Commute.BICYCLE)}"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="B"/>

<RadioButton
    android:checked="@{viewModel.commuteType.equals(Commute.WALKING)}"
    android:onClick="@{()->viewModel.setCommuteType(Commute.WALKING)}"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="W"/>

<RadioButton
    android:checked="@{viewModel.commuteType.equals(Commute.BUS)}"
    android:onClick="@{()->viewModel.setCommuteType(Commute.BUS)}"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="T"/>

答案 2 :(得分:4)

几个小时后,我找到了简单的方法:在android中进行双向数据绑定。它是和Kotlin的基本骨架。您也可以使用ObservableField()

  1. 将您的设置为数据
  2. 根据需要使用按钮创建。重要提示:设置所有单选按钮ID !!!
  3. 通过双向在单选组中设置检查变量(使用viewmodel变量)
  4. 享受)

layout.xml

<data>
    <variable
        name="VM"
        type="...YourViewModel" />
</data>


<LinearLayout
            android:id="@+id/settings_block_env"
            ...
            >


            <RadioGroup

                android:id="@+id/env_radioGroup"
                android:checkedButton="@={VM.radio_checked}">

                <RadioButton
                    android:id="@+id/your_id1"/>

                <RadioButton
                   android:id="@+id/your_id2" />

                <RadioButton
                    android:id="@+id/your_id3"/>

                <RadioButton
                    android:id="@+id/your_id4"/>
            </RadioGroup>

        </LinearLayout>

class YourViewModel(): ViewModel {

var radio_checked = MutableLiveData<Int>()


init{
    radio_checked.postValue(R.id.your_id1)//def value
}

//other code
}

答案 3 :(得分:1)

通常你更关心实际检查的而不是“检查过的东西”。在这种情况下,替代解决方案是忽略RadioGroup并绑定所有项目,如下所示:

Code is : 

.#ifdef _MSC_VER
/*
 * we do not want the warnings about the old deprecated and unsecure CRT functions
 * since these examples can be compiled under *nix as well
 */
.#define _CRT_SECURE_NO_WARNINGS
.#endif

.#include "pcap.h"

/* prototype of the packet handler */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);

int main(int argc, char **argv)
{
    pcap_if_t *alldevs;
    pcap_if_t *d;
    int inum;
    int i=0;
    pcap_t *adhandle;
    char errbuf[PCAP_ERRBUF_SIZE];
    pcap_dumper_t *dumpfile;


    /* Check command line */
    if(argc != 2)
    {
        printf("usage: %s filename", argv[0]);
        return -1;
    }

    /* Retrieve the device list on the local machine */
    if (pcap_findalldevs(&alldevs, errbuf) == -1)
    {
        fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
        return 0;
    }

    /* Print the list */
    for(d=alldevs; d; d=d->next)
    {
        printf("%d. %s", ++i, d->name);
        if (d->description)
            printf(" (%s)\n", d->description);
        else
            printf(" (No description available)\n");
    }

    if(i==0)
    {
        printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
        return -1;
    }

    printf("Enter the interface number (1-%d):",i);
    scanf("%d", &inum);

    if(inum < 1 || inum > i)
    {
        printf("\nInterface number out of range.\n");
        /* Free the device list */
        pcap_freealldevs(alldevs);
        return -1;
    }

    /* Jump to the selected adapter */
    for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);


    /* Open the adapter */
    if ((adhandle= pcap_open_live(d->name,  // name of the device
                             65536,         // portion of the packet to capture.
                                            // 65536 grants that the whole packet will be captured on all the MACs.
                             1,             // promiscuous mode (nonzero means promiscuous)
                             1000,          // read timeout
                             errbuf         // error buffer
                             )) == NULL)
    {
        fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
        /* Free the device list */
        pcap_freealldevs(alldevs);
        return -1;
    }

    /* Open the dump file */
    dumpfile = pcap_dump_open(adhandle, argv[1]);

    if(dumpfile==NULL)
    {
        fprintf(stderr,"\nError opening output file\n");
        return -1;
    }

    printf("\nlistening on %s... Press Ctrl+C to stop...\n", d->description);

    /* At this point, we no longer need the device list. Free it */
    pcap_freealldevs(alldevs);

    /* start the capture */
    pcap_loop(adhandle, 0, packet_handler, (unsigned char *)dumpfile);

    pcap_close(adhandle);
    return 0;
}

int count = 0;
/* Callback function invoked by libpcap for every incoming packet */
void packet_handler(u_char *dumpfile, const struct pcap_pkthdr *header, const u_char *pkt_data)
{

    printf ("\nPacket count is %d", count++);
    /* save the packet on the dump file */
    pcap_dump(dumpfile, header, pkt_data);

    printf("\n\nRecieved Packet Size caplen: %zu", header->caplen); /* Length of header */
    printf("\nRecieved Packet Size just len: %lu\n", header->len); /* Length of header */

}


Output of the program : 


C:\eclipse\w1\tn\Debug>tn pdump
1. \Device\NPF_{3A8147EC-2368-4216-9603-7641E806F7CE} (Realtek PCIe GBE Family Controller)
2. \Device\NPF_{E39C84EC-CB5D-4300-A5C1-C63EDE0B583C} (Microsoft)
3. \Device\NPF_{2F63E116-DEB4-4FE4-9E27-85DCA9E68C5C} (VMware Virtual Ethernet Adapter)
4. \Device\NPF_{996ACB8F-C0AC-4FF3-8E79-B0C849CC9CF1} (MS NDIS 6.0 LoopBack Driver)
5. \Device\NPF_{619BA8D0-363E-4BCB-8739-4537D3FDAD87} (MS NDIS 6.0 LoopBack Driver)
6. \Device\NPF_{E2FB8B54-0E25-498C-975A-32873AC52060} (VMware Virtual Ethernet Adapter)
7. \Device\NPF_{9B17BD4E-E20D-43A3-A8C6-AC8C3C431182} (Microsoft)
Enter the interface number (1-7):2

listening on Microsoft... Press Ctrl+C to stop...

Packet count is 0

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444

Packet count is 1

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444

Packet count is 2

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444

Packet count is 3

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444

Packet count is 4

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444

Packet count is 5

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444

Packet count is 6

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444

Packet count is 7

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444

Packet count is 8

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444

Packet count is 9

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444

Packet count is 10

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444

Packet count is 11

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 12

Recieved Packet Size caplen: 1342177300
Recieved Packet Size just len: 3632886444

Packet count is 13

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 14

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 15

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 16

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 17

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295

Packet count is 18

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295

Packet count is 19

Recieved Packet Size caplen: 1161363476
Recieved Packet Size just len: 4294967295

Packet count is 20

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 6160385

Packet count is 21

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295

Packet count is 22

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295

Packet count is 23

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295

Packet count is 24

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295

Packet count is 25

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 1970834832

Packet count is 26

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444

Packet count is 27

Recieved Packet Size caplen: 4278190100
Recieved Packet Size just len: 4294967295

Packet count is 28

Recieved Packet Size caplen: 16711700
Recieved Packet Size just len: 4294967295

Packet count is 29

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 4294967295

Packet count is 30

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 4294967295

Packet count is 31

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 4294967295

Packet count is 32

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295

Packet count is 33

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295

Packet count is 34

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295

Packet count is 35

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295

Packet count is 36

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 2136866817

Packet count is 37

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 38

Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444

Packet count is 39

Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444

Packet count is 40

Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444

Packet count is 41

Recieved Packet Size caplen: 872349716
Recieved Packet Size just len: 3632886444

Packet count is 42

Recieved Packet Size caplen: 872349716
Recieved Packet Size just len: 3632886444

Packet count is 43

Recieved Packet Size caplen: 4281532436
Recieved Packet Size just len: 3632886444

Packet count is 44

Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444

Packet count is 45

Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444

Packet count is 46

Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444

Packet count is 47

Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444

Packet count is 48

Recieved Packet Size caplen: 4281532436
Recieved Packet Size just len: 3632886444

Packet count is 49

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 50

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 51

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 52

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 53

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 1970834832

Packet count is 54

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 55

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 6160385

Packet count is 56

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 1970834832

Packet count is 57

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 58

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 2136866817

Packet count is 59

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 2136866817

Packet count is 60

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 2136866817

Packet count is 61

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 2136866817

Packet count is 62

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 63

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 64

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 65

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 66

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 67

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 68

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 69

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 70

Recieved Packet Size caplen: 1929379860
Recieved Packet Size just len: 3632886444

Packet count is 71

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 72

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 73

Recieved Packet Size caplen: 5111828
Recieved Packet Size just len: 3632886444

Packet count is 74

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 75

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 76

Recieved Packet Size caplen: 5701652
Recieved Packet Size just len: 3632886444

Packet count is 77

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 78

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 79

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 80

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 81

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 82

Recieved Packet Size caplen: 822083604
Recieved Packet Size just len: 3632886444

Packet count is 83

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 84

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 85

Recieved Packet Size caplen: 1448083476
Recieved Packet Size just len: 3632886444

Packet count is 86

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 87

Recieved Packet Size caplen: 1073741844
Recieved Packet Size just len: 3632886444

Packet count is 88

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 89

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 90

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 91

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 92

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 93

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 94

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 1970834832

Packet count is 95

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 96

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 1970834832

Packet count is 97

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 1970834832

Packet count is 98

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 1970834832

Packet count is 99

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 100

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 1970834832

Packet count is 101

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 1970834832

Packet count is 102

Recieved Packet Size caplen: 872349716
Recieved Packet Size just len: 1970834832

Packet count is 103

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 104

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 1970834832

Packet count is 105

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 106

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 107

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 1970834832

Packet count is 108

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 109

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 3632886444

Packet count is 110

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 1970834832

Packet count is 111

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444

Packet count is 112

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444

Packet count is 113

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 1970834832

Packet count is 114

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 3632886444

Packet count is 115

Recieved Packet Size caplen: 562102292
Recieved Packet Size just len: 1970834832

Packet count is 116

Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444

Packet count is 117

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 1970834832

Packet count is 118

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 1970834832

Packet count is 119

Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444

Packet count is 120

Recieved Packet Size caplen: 4281532436
Recieved Packet Size just len: 3632886444

Packet count is 121

Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 1970834832

Packet count is 122

Recieved Packet Size caplen: 872349716
Recieved Packet Size just len: 3632886444

Packet count is 123

Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444

Packet count is 124

Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444

Packet count is 125

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 1970834832

Packet count is 126

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 1970834832

Packet count is 127

Recieved Packet Size caplen: 4281532436
Recieved Packet Size just len: 3632886444

Packet count is 128

Recieved Packet Size caplen: 4294901780
Recieved Packet Size just len: 1970834832

Packet count is 129

Recieved Packet Size caplen: 4281532436
Recieved Packet Size just len: 3632886444

Packet count is 130

Recieved Packet Size caplen: 4281532436
Recieved Packet Size just len: 3632886444

Packet count is 131

Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444

Packet count is 132

Recieved Packet Size caplen: 872349716
Recieved Packet Size just len: 1970834832

Packet count is 133

Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444

Packet count is 134

Recieved Packet Size caplen: 4281532436
Recieved Packet Size just len: 3632886444

Packet count is 135

Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 4294967295

Packet count is 136

Recieved Packet Size caplen: 872349716
Recieved Packet Size just len: 4294967295

Packet count is 137

Recieved Packet Size caplen: 4281532436
Recieved Packet Size just len: 3632886444

Packet count is 138

Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 4294967295

Packet count is 139

Recieved Packet Size caplen: 872349716
Recieved Packet Size just len: 4294967295

Packet count is 140

Recieved Packet Size caplen: 4281532436
Recieved Packet Size just len: 4294967295

Packet count is 141

Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444

Packet count is 142

Recieved Packet Size caplen: 872349716
Recieved Packet Size just len: 4294967295

Packet count is 143

Recieved Packet Size caplen: 4281532436
Recieved Packet Size just len: 4294967295

Packet count is 144

Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 4294967295

Packet count is 145

Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444

Packet count is 146

Recieved Packet Size caplen: 4281532436
Recieved Packet Size just len: 4294967295

Packet count is 147

Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 4294967295

Packet count is 148

Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 3632886444

Packet count is 149

Recieved Packet Size caplen: 4281532436
Recieved Packet Size just len: 4294967295

Packet count is 150

Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 4294967295

Packet count is 151

Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 4294967295

Packet count is 152

Recieved Packet Size caplen: 858980372
Recieved Packet Size just len: 4294967295

Packet count is 153

Recieved Packet Size caplen: 872349716
Recieved Packet Size just len: 4294967295

Packet count is 154

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295

Packet count is 155

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295

Packet count is 156

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295

Packet count is 157

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295

Packet count is 158

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295

Packet count is 159

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295

Packet count is 160

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295

Packet count is 161

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295

Packet count is 162

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295

Packet count is 163

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295

Packet count is 164

Recieved Packet Size caplen: 20
Recieved Packet Size just len: 4294967295

其中optionA,optionB和optionC在ViewModel中定义,如下所示:

<RadioGroup (...) >
        <RadioButton (...)
            android:checked="@={viewModel.optionA}"/>

        <RadioButton (...)
            android:checked="@={viewModel.optionB}"/>

        <RadioButton (...)
            android:checked="@={viewModel.optionC}"/>
</RadioGroup>

这通常就足够了,但是如果您想立即对点击作出反应,那么您可以添加callBack并使用它们:

public final ObservableBoolean optionA = new ObservableBoolean();
public final ObservableBoolean optionB = new ObservableBoolean();
public final ObservableBoolean optionC = new ObservableBoolean();

这种方法的优点是您不需要比较和跟踪“id”。

答案 4 :(得分:0)

在我当前的项目中,我是这样做的。 我在该项目中有三种货币,我可以通过RadioGroup选择一种货币。

带有货币的枚举:

enum class Currency(val value: Byte) {
    USD(0),
    EUR(1),
    RUB(2);

    companion object Create {
        fun from(sourceValue: Byte): Currency = values().first { it.value == sourceValue }
        fun from(sourceValue: String): Currency = values().first { it.toString() == sourceValue }
    }
}

我的ViewModel的一部分:

    class BaseCurrencyViewModel : ViewModelBase<BaseCurrencyModelInterface>() {
        /**
         * Selected currency
         */
        val currency: MutableLiveData<Currency> = MutableLiveData()

        /**
         *
         */
        init {
            currency.value = Currency.USD   // Init value
        }
  }

我的部分布局(注意RadioGroup和RadioButton标签的绑定)

<RadioGroup
    android:id="@+id/currencySwitchers"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    app:selectedCurrency = "@{viewModel.currency}"

    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintEnd_toEndOf="parent">

    <RadioButton
        android:id="@+id/usdSwitcher"

        android:text="USD"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"

        android:tag="USD"
    />

    <RadioButton
        android:id="@+id/eurSwitcher"

        android:text="EUR"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"

        android:tag="EUR"
    />

    <RadioButton
        android:id="@+id/rubSwitcher"

        android:text="RUB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"

        android:tag="RUB"
    />
</RadioGroup>

最后一部分-绑定适配器。

@BindingAdapter("selectedCurrency")
fun setSelectedCurrency(view: View, value: MutableLiveData<Currency>?) {
    view.getParentActivity()?.let { parentActivity ->
        value?.observe(parentActivity, Observer { value ->
            view.findViewWithTag<RadioButton>(value.toString())
                ?.also {
                    if(!it.isChecked) {
                        it.isChecked = true
                    }
                }
            }
        )

        (view as RadioGroup).setOnCheckedChangeListener { radioGroup, checkedId ->
            val currency = Currency.from(radioGroup.findViewById<RadioButton>(checkedId).tag as String)
            if(value != null && value.value != currency) {
                value.value = currency
            }
        }
    }
}

通过这种方式,我在RadioGroup和ViewModel中的属性之间进行了双向绑定。