简体   繁体   English

如何将值分配给按钮并设置onClickListener

[英]How to assign value into button and set the onClickListener

在此处输入图片说明
I would like to create a Pop-Out dialog which will be similar like above picture. 我想创建一个弹出对话框,类似于上图。 All the button will have the text from a List of texts and when any specific button is pressed the respective image should load in the Image section below. 所有按钮都将具有文本列表中的文本,当按下任何特定按钮时,相应的图像应加载到下面的“图像”部分中。

For example: I have the JSON data like shown below, Once the user clicks Apple button the Image section below should load the link https://test.com/images/apple.jpg . 例如:我具有如下所示的JSON数据,一旦用户单击Apple按钮,下面的图像部分应加载链接https://test.com/images/apple.jpg Any idea or tutorial to achieve this? 任何想法或教程来实现这一目标?

"Fruit": [
{
  "Title": "Apple",
  "ImageURL": "https://test.com/images/apple.jpg"
},
{
  "Title": "Orange",
  "ImageURL": "https://test.com/images/orange.jpg"
},
{
  "Title": "Banana",
  "ImageURL": "https://test.com/images/banana.jpg"
},
{
  "Title": "Watermelon",
  "ImageURL": "https://test.com/images/watermelon.jpg"
},
{
  "Title": "Strawberry",
  "ImageURL": "https://test.com/images/strawberry.jpg"
}

add button into Linear layout using for loop Like this. 使用for循环将按钮添加到线性布局中。

LinearLayout layout=(LinearLayout )findViewById(R.id.layout);
        for(int i=0;i<fruitList.size();i++)
        {
            final Button button = new Button(this);
            button.setText(fruitList.get(i).getName());
            button.setId(i);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position=button.getId();
                    imageView.setImageURI(fruitList.get(position).getImage());
                }
            });
            layout.addView(button);

        }

here is github library instand of linear layout you can use FlexboxLayout of this library to add multiple row with button. 这是github库了解线性布局,您可以使用此库的FlexboxLayout通过按钮添加多行。 https://github.com/google/flexbox-layout https://github.com/google/flexbox-layout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM