2012-02-19 81 views
0

我是Android开发人员的开工者。我能够实现textSwitcher和ViewSwitcher,但单独在一个活动中。我希望他们在同一活动中发挥作用。如何在同一活动中使用ViewFlipper(或)ViewSwitcher和TextSwitcher ..?

此代码不会给我IDE中的错误,但它在模拟器中的“强制关闭”。

进口后的代码和所有..

ViewFlipper vS1,vS2,vS3; 
Button onep,twop,lose,win,help,settings,start,pick1,pick2; 
int condn,total,max=5,min=1,rem; 
TextSwitcher minte,maxte,select1,select2,coinsrem; 

@Override 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    // Animations 

    Animation in = AnimationUtils.loadAnimation(this, 
      android.R.anim.slide_in_left); 
    Animation out = AnimationUtils.loadAnimation(this, 
      android.R.anim.slide_out_right); 

// Text Views ---- 
    minte.findViewById(R.id.min); 
    maxte.findViewById(R.id.max); 

    minte.setFactory(new TextSwitcherFactory()); 
    maxte.setFactory(new TextSwitcherFactory()); 

    minte.setText("Hello"); 

    // switching Views ---- 

    vS1 =(ViewFlipper) findViewById(R.id.vs1); 
    vS2 =(ViewFlipper) findViewById(R.id.vs2); 
    vS3 =(ViewFlipper) findViewById(R.id.vs3); 

    vS1.setInAnimation(in); 
    vS1.setOutAnimation(out); 
    vS2.setInAnimation(in); 
    vS2.setOutAnimation(out); 
    vS3.setInAnimation(in); 
    vS3.setOutAnimation(out); 

// create RangeSeekBar as Integer range between 20 and 75 
    RangeSeekBar<Integer> seekBar = new RangeSeekBar<Integer>(20, 75, getBaseContext()); 
    seekBar.setOnRangeSeekBarChangeListener(this); 

    // add RangeSeekBar to pre-defined layout 
    ViewGroup layout = (ViewGroup) findViewById(R.id.rangeseek); 
    layout.addView(seekBar); 

    //----buttons------------------------- 

    win = (Button)findViewById(R.id.win); 
    lose = (Button)findViewById(R.id.loses); 

    win.setOnClickListener(this); 
    lose.setOnClickListener(this); 

    //----buttons end---------------------- 
    //---Animating the views--- 



} 







@Override 
public void rangeSeekBarValuesChanged(Integer minValue, Integer maxValue) { 
    // TODO Auto-generated method stub 

} 







public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch(v.getId()) 
    { 

    case R.id.win: 
     condn = 1; 
     vS2.showNext(); 

     break; 
    case R.id.loses : 
     condn =0; 
     vS2.showNext(); 

     break; 



    } 
} 

public class TextSwitcherFactory implements ViewSwitcher.ViewFactory{ 

    @Override 
    public View makeView() { 
     // TODO Auto-generated method stub 
     TextView t = new TextView(LastActivity.this); 
     t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL); 
     t.setTextSize(36); 
     t.setTextColor(Color.argb(255, 255, 204, 51)); 
     return t;  } 

} 

回答

1
minte.findViewById(R.id.min); 
maxte.findViewById(R.id.max); 

没有意义......

你应该initialaze他们想:

minte =(TextSwitcher) findViewById(R.id.min); 
0

从你代码,它看起来像你试图初始化3个独立的ViewFlippers并在它们之间切换。这不是如何做到这一点。 ViewFlipper(或ViewSwitcher)是FrameLayout类的扩展。因此,它一次只会显示1个子视图。

要修复您的代码,请只定义一个ViewFlipper。在那之下,定义你想要切换的3个视图(即RelativeLayout,LinearLayout等)。

此外,它看起来像你也试图做一些动画。所以,你最终可能会想使用ViewAnimator类,而这只是ViewFlipper的父类。

谷歌的一些教程。那里有一些很好的东西。

我希望这有助于!

相关问题