ImageButton Android切换bg图片

时间:2013-08-30 21:01:24

标签: android

谁能告诉我为什么这不起作用?我没有任何错误,但根本没有工作。

在我添加ScreenSwipe之前,这实际上工作得很好,现在我无法弄清楚什么是错的。

由于

package com.sample.sample

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

public class PageOneFragment extends Fragment {

ImageButton btn1;
static int i = 0;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
        R.layout.page1_layout, container, false);
btn1 = (ImageButton) rootView.findViewById(R.id.m1_btn);
return rootView;
}

public void setupPlayPauseButton1(View view) {
    btn1 = (ImageButton) view.findViewById(R.id.m1_btn);

    btn1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            i +=1;
            if (i % 2 == 0) {
                btn1.setImageResource(R.drawable.pause);
            } else {
                btn1.setImageResource(R.drawable.play);

            }
        }
    });
}
}

修改

这是对的吗?

package com.wepromoteme.brapp;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

public class PageOneFragment extends Fragment {

ImageButton btn1;
int i = 0;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
        R.layout.page1_layout, container, false);
btn1 = (ImageButton) rootView.findViewById(R.id.m1_btn);


setupPlayPauseButton1(rootView);

return rootView;

}

private void setupPlayPauseButton1(View view) {
    btn1 = (ImageButton) view.findViewById(R.id.m1_btn);

    btn1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            i +=1;
            if (i % 2 == 0) {
                btn1.setImageResource(R.drawable.pause);
            } else {
                btn1.setImageResource(R.drawable.play);

            }
        }   
    });
}
}

2 个答案:

答案 0 :(得分:1)

你没有说出什么“不起作用”,但我愿意猜测这是原因:static int i = 0;

静态此处表示i的所有实例共享PageOneFragment的值。

如果在旋转屏幕等时使用静态来保持变量值,则应使用片段onSaveInstanceState()方法。您可以在官方文档中找到有关此内容的更多信息: Handling the Fragment Lifecycle

答案 1 :(得分:0)

碰巧我已经弄明白了,我决定让我的大脑有几个小时休息,因为我觉得我陷入了无法看到树木的向下螺旋:P

以下是我遇到同样问题的其他人的代码。这实际上有效:

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

public class PageTwoFragment extends Fragment {

int i = 0;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
        R.layout.page2_layout, container, false);

final ImageButton btn1 = (ImageButton) rootView.findViewById(R.id.m1_btn);
final ImageButton btn2 = (ImageButton) rootView.findViewById(R.id.m2_btn);
final ImageButton btn3 = (ImageButton) rootView.findViewById(R.id.m3_btn);
final ImageButton btn4 = (ImageButton) rootView.findViewById(R.id.m4_btn);
final ImageButton btn5 = (ImageButton) rootView.findViewById(R.id.m5_btn);

btn1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        i +=1;
        if (i % 2 == 0) {
            btn1.setImageResource(R.drawable.pause);
        } else {
            btn1.setImageResource(R.drawable.play);
        }
    }
});

btn2.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        i +=1;
        if (i % 2 == 0) {
            btn2.setImageResource(R.drawable.pause);
        } else {
            btn2.setImageResource(R.drawable.play);
        }
    }
});

btn3.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        i +=1;
        if (i % 2 == 0) {
            btn3.setImageResource(R.drawable.pause);
        } else {
            btn3.setImageResource(R.drawable.play);
        }

    }
});

btn4.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        i +=1;
        if (i % 2 == 0) {
            btn4.setImageResource(R.drawable.pause);
        } else {
            btn4.setImageResource(R.drawable.play);
        }

    }
});

btn5.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        i +=1;
        if (i % 2 == 0) {
            btn5.setImageResource(R.drawable.pause);
        } else {
            btn5.setImageResource(R.drawable.play);
            }       
    }
});

return rootView;
}
}