Viewflipper onclick无效

时间:2012-12-19 18:30:09

标签: android

场景:我有一个视图lobby4.xml,里面有一个ViewFlipper和两个RelativeLayout。 两个relativelayouts都有一个名为buttonTOG的按钮,我想用它来从一个relativelayout转换到另一个。这是代码:

    @Override
public void onCreate(Bundle savedInstanceState) {


  super.onCreate(savedInstanceState);
  setContentView(R.layout.lobby4);
  Context context = getApplicationContext();
  final ViewFlipper vf = (ViewFlipper) findViewById( R.id.viewFlipper );
  bTOG=(Button)findViewById(R.id.buttonTOG);
  final Activity the_a = this;
  final Context static_ctx = this.getApplicationContext();
    bTOG.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
          String buttons=bTOG.getText().toString();
          bTOG=(Button)findViewById(R.id.buttonTOG);
          String mystring = getResources().getString(R.string.Playbyemail);
          if( buttons!=null && buttons.compareTo(mystring)==0){
              vf.showPrevious();  
          }else{
              vf.showNext();  
          }


      }
    });


}

当我在bTOG上第二次点击时,没有onClick事件。所以我想我必须注册onclick?

1 个答案:

答案 0 :(得分:0)

应该足够了:

@Override
public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.lobby4);
final ViewFlipper vf = (ViewFlipper) findViewById( R.id.viewFlipper );
bTOG=(Button)findViewById(R.id.buttonTOG);
  bTOG.setOnClickListener(new View.OnClickListener() {
  public void onClick(View v) {
      if(vf.getDisplayedChild() == 0){
vf.setDisplayedChild(1);
}else{
vf.setDisplayedChild(0)
}


  }
 });


}