0

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

在我添加 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);

            }
        }   
    });
}
}
4

2 回答 2

1

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

这里的静态意味着 的值由 的所有实例i共享。PageOneFragment

如果你在屏幕旋转等情况下使用静态来保持变量值,你应该使用片段onSaveInstanceState()方法来代替。您可以在官方文档中找到有关此的更多信息: 处理片段生命周期

于 2013-08-30T21:17:30.673 回答
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;
}
}
于 2013-08-31T12:50:53.437 回答