Android 根据int值更改textswitcher/viewswitcher的textColor?

Android 根据int值更改textswitcher/viewswitcher的textColor?,android,performance,android-fragments,timer,Android,Performance,Android Fragments,Timer,我有一个从60000毫秒开始的倒计时,当时间在10000毫秒及以下时,我想将文本颜色从color.BLUE更改为color.RED。我试过以下方法,但没有成功;试图设置TextSwitcher的XtColor并添加将根据int值timerState更改颜色的IF语句。。我不知道如何使它工作,除了可能停止计时器,并在毫秒达到10000时创建另一个计时器,这实际上导致了我的第二个问题,其中: 我单击一个imageButton,它启动一个对话框片段(PauseFragment),并通过timerCDT

我有一个从60000毫秒开始的倒计时,当时间在10000毫秒及以下时,我想将文本颜色从color.BLUE更改为color.RED。我试过以下方法,但没有成功;试图设置TextSwitcher的XtColor并添加将根据int值timerState更改颜色的IF语句。。我不知道如何使它工作,除了可能停止计时器,并在毫秒达到10000时创建另一个计时器,这实际上导致了我的第二个问题,其中:

我单击一个imageButton,它启动一个对话框片段(PauseFragment),并通过timerCDT.cancel()调用我的倒计时程序上的cancel()。我遇到了一些null指针问题,因此if语句在我的代码中检查null,但是现在一旦PauseFragment取消,我的新计时器将从60000开始,而不是从上次停止的位置开始。我希望每次调用onTick()时,long-timerState=60000都能更新到millisuntillfinished,但我不确定哪里出错了

因此,有人可以帮助我动态更改TextSwitcher文本颜色,并帮助我找出为什么我的倒计时没有按预期值启动。非常感谢您的帮助

提前谢谢

 public class GameActivity extends FragmentActivity  implements PauseFragment.FragmentCommunicator,{

 public static long timerState = 60000;
    public static boolean isTimerOn = false;
    private String modeChoice = ModesActivity.mode;
    private TextSwitcher timerTextSwitcher;
    CountDownTimer timerCDT;


  @Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

//...more code

 timerTextSwitcher = (TextSwitcher) findViewById(R.id.timerTextSwitcher);
        timerTextSwitcher.setFactory(new ViewSwitcher.ViewFactory() {

            public View makeView() {
                // Create a new TextView and set properties
                TextView textView = new TextView(getApplicationContext());
                textView.setLayoutParams(new TextSwitcher.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                textView.setTextSize(20);
                textView.setTextColor(Color.BLUE);
                if (timerState < 10001) {
                    textView.setTextColor(Color.RED);
                }
                return textView;
            }
        });

  // Declare the animations and initialize them
        Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
        Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
        // set the animation type to textSwitcher
        timerTextSwitcher.setInAnimation(in);
        timerTextSwitcher.setInAnimation(out);
    }
    timerCDT = new CountDownTimer(timerState, 1000) {
        public void onTick(long millisUntilFinished) {
            isTimerOn = true;
            timerTextSwitcher.setText(String.valueOf(millisUntilFinished / 1000));
            timerState = millisUntilFinished;
        }

        //TODO: assign highscores for players to beat
        public void onFinish() {
            timerTextSwitcher.post(new Runnable() {
                @Override
                public void run() {
                    createToast("GAME OVER!");
                }
            });
            isTimerOn = false;

            DialogFragment endDialog = new EndGameFragment();
            endDialog.show(getSupportFragmentManager(), "EndGameDialogFragment");
        }
    };
    timerCDT.start();


 @Override
public void onPause() {
    super.onPause();
    Bundle args = new Bundle();
    args.putInt(ARG_SCORE, scoreINT);
    args.putLong(ARG_TIMER, timerState);
    args.putString(GameActivity.ARG_MODE, modeChoice);
    if (timerCDT != null) {
        timerCDT.cancel();
    }
    else{
        createToastExtended("onPause() - timerCDT is null; attempt to cancel");
    }
}

 //.!.other fun code here.!.

 @Override
 protected void onStop() {
    super.onStop();
    if (timerCDT != null) {
        timerCDT.cancel();
    }
    else{
        createToastExtended("onStop() - timerCDT is null; attempt to cancel");
    }

 }

 //Player Response information
 @Override
 public void pauseFragmentResponse() {
    if (timerCDT != null) {
        timerCDT.start();
    }
    else{
        createToastExtended("pauseFragmenResponse() - timerCDT is null; attempt to start");
    }
}

 public void pauseStartFrag(View view) {
    DialogFragment dialog = new PauseFragment();
    if (timerCDT != null) {
        timerCDT.cancel();
    }
    else{
        createToastExtended("pauseStartFrag() - timerCDT is null;attempt to cancel");
    }
    dialog.show(getSupportFragmentManager(), "PauseDialogFragment");
 }


 // Code for PauseFragment

 //TODO: remove unuses imports on all files within project; 

 import android.app.Activity;
 import android.app.AlertDialog;
 import android.app.Dialog;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.os.Bundle;
 import android.support.v4.app.DialogFragment;
 import android.view.LayoutInflater;

 public class PauseFragment extends DialogFragment {

public static boolean isPaused = false;
public FragmentCommunicator fComm;


@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        fComm = (FragmentCommunicator) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement FragmentCommunicator");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    fComm = null;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    isPaused = true;
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.fragment_pause, null))
           .setMessage(R.string.dialog_pause)
           .setPositiveButton(R.string.action_main_menu, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   Intent i4 = new Intent(getActivity(), StartActivity.class);
                   startActivity(i4);
               }
           })
           .setNeutralButton(R.string.action_restart, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   Intent i4 = new Intent(getActivity(), ModesActivity.class);
                   startActivity(i4);                   }
           })
           .setNegativeButton(R.string.action_resume, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // User cancelled the dialog
                   fComm.pauseFragmentResponse();
                   dismiss();
               }
           });
    // Create the AlertDialog object and return it
    return builder.create();
}

@Override
public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);
    isPaused = false;
}

public interface FragmentCommunicator {
    public void pauseFragmentResponse();
}
 }
公共类GameActivity扩展FragmentActivity实现PauseFragment.FragmentCommunicator{
公共静态长时间状态=60000;
公共静态布尔值isTimerOn=false;
私有字符串modeChoice=ModesActivity.mode;
专用文本切换器timerTextSwitcher;
倒计时定时器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//…更多代码
timerTextSwitcher=(TextSwitcher)findviewbyd(R.id.timerTextSwitcher);
timerTextSwitcher.setFactory(新的ViewSwitcher.ViewFactory(){
公共视图makeView(){
//创建新的TextView并设置属性
TextView TextView=新的TextView(getApplicationContext());
textView.setLayoutParams(新的TextSwitcher.LayoutParams(ViewGroup.LayoutParams.WRAP_内容,ViewGroup.LayoutParams.WRAP_内容));
textView.setTextSize(20);
textView.setTextColor(Color.BLUE);
如果(时间状态<10001){
textView.setTextColor(Color.RED);
}
返回文本视图;
}
});
//声明动画并初始化它们
Animation in=AnimationUtils.loadAnimation(这个,android.R.anim.slide\u在左);
Animation out=AnimationUtils.loadAnimation(这是android.R.anim.slide\u out\u right);
//将动画类型设置为textSwitcher
TimerExtSwitcher.setInAnimation(输入);
TimerExtSwitcher.setInAnimation(输出);
}
timerCDT=新的倒计时(timerState,1000){
公共void onTick(长毫秒未完成){
isTimerOn=真;
timerTextSwitcher.setText(String.valueOf(毫秒直到完成/1000));
timerState=毫秒,直至完成;
}
//TODO:为要击败的玩家分配高分
公共无效onFinish(){
timerTextSwitcher.post(新的Runnable(){
@凌驾
公开募捐{
CreateToos(“游戏结束!”);
}
});
isTimerOn=假;
DialogFragment endDialog=新的EndGameFragment();
显示(getSupportFragmentManager(),“EndGameDialogFragment”);
}
};
timerCDT.start();
@凌驾
公共无效暂停(){
super.onPause();
Bundle args=新Bundle();
args.putInt(ARG_分数,scoreINT);
args.putLong(ARG_TIMER,timerState);
args.putString(GameActivity.ARG_MODE,modeChoice);
如果(timerCDT!=null){
timerCDT.cancel();
}
否则{
createToastExtended(“onPause()-timerCDT为空;尝试取消”);
}
}
//.!.这里还有其他有趣的代码。!。
@凌驾
受保护的void onStop(){
super.onStop();
如果(timerCDT!=null){
timerCDT.cancel();
}
否则{
createToastExtended(“onStop()-timerCDT为null;尝试取消”);
}
}
//播放器响应信息
@凌驾
public void pauseFragmentResponse(){
如果(timerCDT!=null){
timerCDT.start();
}
否则{
createToastExtended(“pauseFragmenResponse()-timerCDT为空;尝试启动”);
}
}
公共空间pauseStartFrag(视图){
DialogFragment dialog=新建PauseFragment();
如果(timerCDT!=null){
timerCDT.cancel();
}
否则{
createToastExtended(“pauseStartFrag()-timerCDT为null;尝试取消”);
}
show(getSupportFragmentManager(),“PauseDialogFragment”);
}
//暂停触发代码
//TODO:删除项目中所有文件上未使用的导入;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.app.Dialog;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.os.Bundle;
导入android.support.v4.app.DialogFragment;
导入android.view.LayoutInflater;
公共类PauseFragment扩展了DialogFragment{
公共静态布尔值isPaused=false;
公共碎片通讯器fComm;
@凌驾
公共事务主任(活动){
超级转速计(活动);
试一试{
fComm=(碎片通讯器)活动;
}catch(ClassCastException e){
抛出新的ClassCastException(activity.toString()
+“必须实施碎片化通讯器”);
}
}
@凌驾
公共无效连接(){
super.onDetach();
fComm=null;
}
@凌驾
创建对话框上的公共对话框(Bundle savedInstanceState){
isPaused=真;
//使用生成器类可以方便地构造对话框
AlertDialog.Builder=新建AlertDialog.Builder(getActivity());
//被解雇
private TextSwitcher TextSw;
private TextView TextSwTextView;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(com.game.test.R.layout.sample);


    TextSw = (TextSwitcher) findViewById(R.id.TextSwitchView);
    TextSw.setFactory(new ViewSwitcher.ViewFactory() 
    {

        public View makeView() 
        {
            // Create a new TextView and set properties
            TextView textView = new TextView(getApplicationContext());
            textView.setLayoutParams(new TextSwitcher.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            textView.setTextSize(20);
            textView.setTextColor(Color.BLUE);

            return textView;
        }
    });
    mtimer = new CountDownTimer(60000, 1000) 
    {

        public void onTick(long millisUntilFinished) 
        {
            TextSwTextView = (TextView) TextSw.getChildAt(0); 
            if(millisUntilFinished < 10001)
                TextSwTextView.setTextColor(Color.RED);
            TextSwTextView.setText("seconds remaining: " + millisUntilFinished / 1000);
        }

        public void onFinish()
        {
            TextSwTextView.setText("done!");
        }
     }.start();

}
TextView t1 = (TextView) mSwitcher.getChildAt(0); 
TextView t2 = (TextView) mSwitcher.getChildAt(1); 
textSwitcher = (TextSwitcher) findViewById(R.id.textView99);
textSwitcher.setInAnimation(this, R.anim.slide_in_right);
textSwitcher.setOutAnimation(this, R.anim.slide_out_left);
t1 = new TextView(this);
t2 = new TextView(this);
t1.setTextSize(20);
t2.setTextSize(20);
textSwitcher.addView(t1);
textSwitcher.addView(t2);