首页 > 解决方案 > 在运行时更改图像按钮

问题描述

我在工具栏上有一个 imagebutton 作为 img1 单击它会显示包含 imagebuttons 作为 img2、img3、img4 等的线性布局。现在,当我单击 img2 时,工具栏上的 img1 按钮应更改为 img2。我怎样才能做到这一点?我在clickListener上尝试了几个不同的步骤,但没有奏效..

任何帮助,将不胜感激?

我已经搜索过这个问题的答案,我知道如何将一个图像设置为另一个图像,但我想设置用户是否从线性布局中选择任何按钮。谢谢

在 Activity 类中,我有这段代码

private void showHideAnnotatePanel() {
    ImageButton im = (ImageButton)findViewById(R.id.button_pen);
    if (annotatePanel == null) {
        annotatePanel = new AnnotatePanel(this);

        LinearLayout ll = (LinearLayout)findViewById(R.id.toolbarLayout);
        ll.addView(annotatePanel, 0);
        ll.setVisibility(View.VISIBLE);
        this.annotateShowing = true;

        if (im != null) {
            im.setImageResource(R.drawable.paintpalette_64_wmblue);
        }
    }
    else if (annotateShowing == false) {
        LinearLayout ll = (LinearLayout)findViewById(R.id.toolbarLayout);
        ll.setVisibility(View.VISIBLE);
        annotateShowing = true;
        if (im != null) {
            im.setImageResource(R.drawable.paintpalette_64_wmblue);
        }
    }
    else {
        LinearLayout ll = (LinearLayout)findViewById(R.id.toolbarLayout);
        ll.setVisibility(View.GONE);
        annotateShowing = false;
        if (im != null) {
            im.setImageResource(R.drawable.brush_icon_selector);
        }
    }
}

the new_annotate_screen.xml file is



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#323232"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
    android:background="#323232"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/col_one"
        android:layout_width="305dp"
        android:layout_height="50dip"
        android:orientation="horizontal" >

        <!--  -->
        <ImageButton
            android:id="@+id/new_red_col"
            android:layout_width="40dip"
            android:layout_height="40dip"
            android:layout_margin="5dip"
            android:background="#ff0000" />

        <ImageButton
            android:id="@+id/new_green_col"
            android:layout_width="40dip"
            android:layout_height="40dip"
            android:layout_margin="5dip"
            android:background="#00ff00" />
       <ImageButton
            android:id="@+id/new_blue_col"
            android:layout_width="40dip"
            android:layout_height="40dip"
            android:layout_margin="5dip"
            android:background="#0000ff" />

        <ImageButton
            android:id="@+id/new_yellow_col"
            android:layout_width="40dip"
            android:layout_height="40dip"
            android:layout_margin="5dip"
            android:background="#ffff00" />


 </LinearLayout>




</LinearLayout>

标签: android

解决方案


//considering R.drawable.paintpalette_64_wmblue is the img2
    img2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            img1.setImageResource(R.drawable.paintpalette_64_wmblue);
        }
    });

推荐阅读