如何在单击按钮时切换背景图像?

发布于 2025-01-03 09:40:50 字数 464 浏览 1 评论 0原文

我有这样的代码:

button1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        button1.setBackgroundResource(R.drawable.detailpressed);
        Chapter_sync.add(chapid);
    }

我想要做的是切换以下 clicklistener 中调用的所有方法。

例如,当我第一次单击此按钮时,将调用 setBackgroundResource(R.drawable.detailpressed) ,下次单击时将使用不同的可绘制对象调用相同的方法。

像切换按钮之类的东西。 有人擅长这个请帮忙吗?

I have this code:

button1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        button1.setBackgroundResource(R.drawable.detailpressed);
        Chapter_sync.add(chapid);
    }

What I am trying to do is toggle all the methods called in the following clicklistener.

Eg first time when I Click this button the setBackgroundResource(R.drawable.detailpressed) is called and on the next click same metod is called with different drawable.

Something like toggle button.
Someone good at this plz help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

很酷又爱笑 2025-01-10 09:40:50

您可以采用一个变量,

int i=0;

它会随着每次点击而增加。

if(i%2==0)
   set one image
else 
   set another image

you can take a variable

int i=0;

it will increase with every click.

if(i%2==0)
   set one image
else 
   set another image
月亮是我掰弯的 2025-01-10 09:40:50

创建可绘制对象 ID 的数组并保存索引怎么样:

private final int[] myDrawables = {R.drawable.detailpressed, R.drawable.detailpressed1, ...};
//...
button1.setOnClickListener(new OnClickListener() {
    int index = 0;
    @Override
    public void onClick(View v) {
        button1.setBackgroundResource(myDrawables[index++ % myDrawables.length]);
        Chapter_sync.add(chapid);
    }
}

How about creating an array of the drawable's IDs and saving an index:

private final int[] myDrawables = {R.drawable.detailpressed, R.drawable.detailpressed1, ...};
//...
button1.setOnClickListener(new OnClickListener() {
    int index = 0;
    @Override
    public void onClick(View v) {
        button1.setBackgroundResource(myDrawables[index++ % myDrawables.length]);
        Chapter_sync.add(chapid);
    }
}
怎言笑 2025-01-10 09:40:50

将变量声明为

boolean isOddClicked = true;

并将您的点击侦听器更新为

button1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //Do stuff here for chnaging background of button
            if(isOddClicked) {
                button1.setBackgroundResource(R.drawable.detailpressed);
                isOddClicked = false;
            } else {
                button1.setBackgroundResource(R.drawable.detailpressed_SECOND_IMAGE);
                isOddClicked = true;
            }

            //Do your task
            Chapter_sync.add(chapid);
        }

注意:如果您的要求在两个图像之间移动,那么您可以使用切换按钮并自定义它。它将按照您的要求工作。

declare variable as

boolean isOddClicked = true;

And update your click listener as

button1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //Do stuff here for chnaging background of button
            if(isOddClicked) {
                button1.setBackgroundResource(R.drawable.detailpressed);
                isOddClicked = false;
            } else {
                button1.setBackgroundResource(R.drawable.detailpressed_SECOND_IMAGE);
                isOddClicked = true;
            }

            //Do your task
            Chapter_sync.add(chapid);
        }

NOTE: If your requirement moves between two images then you can use toggle button and customize it. It will work for same as your requirement.

来日方长 2025-01-10 09:40:50

如果xml如下

    <code>

        <Button
            android:id="@+id/btnListView"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:background="@drawable/list_view"
            android:onClick="switchToListView"
            android:visibility="visible"
            />

        <Button
            android:id="@+id/btnGridView"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:background="@drawable/grid_view"
            android:onClick="switchToGridView"
            android:visibility="gone"
            />
    <code>


处理代码就像

<code>
public void switchToListView(View view) {
    (Button) findViewById(R.id.btnListView).setVisibility(View.GONE);
    (Button) findViewById(R.id.btnGridView).setVisibility(View.VISIBLE);
}
public void switchToGridView(View view) {
    (Button) findViewById(R.id.btnGridView).setVisibility(View.GONE);
    (Button) findViewById(R.id.btnListView).setVisibility(View.VISIBLE);

}

</code>

if xml as the following

    <code>

        <Button
            android:id="@+id/btnListView"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:background="@drawable/list_view"
            android:onClick="switchToListView"
            android:visibility="visible"
            />

        <Button
            android:id="@+id/btnGridView"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:background="@drawable/grid_view"
            android:onClick="switchToGridView"
            android:visibility="gone"
            />
    <code>

handling Code will be like

<code>
public void switchToListView(View view) {
    (Button) findViewById(R.id.btnListView).setVisibility(View.GONE);
    (Button) findViewById(R.id.btnGridView).setVisibility(View.VISIBLE);
}
public void switchToGridView(View view) {
    (Button) findViewById(R.id.btnGridView).setVisibility(View.GONE);
    (Button) findViewById(R.id.btnListView).setVisibility(View.VISIBLE);

}

</code>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文