0

我正在尝试做一个具有 ViewFlipper 和下一个按钮的测验应用程序,我想禁用下一个按钮或让它执行其他命令,例如在最后一个视图时启动新活动,这可能吗?

Java 文件

    Button Result = (Button) findViewById(R.id.Button);
    Button Next= (Button) findViewById(R.id.Next);
    ViewFlipper flipp = (ViewFlipper) findViewById(R.id.flipp);

    if(flipp.getDisplayedChild() == flipp.getChildCount()){
       Next.setText("Result");
    }
    Next.setOnClickListener(new View.OnClickListener() {
        ViewFlipper flipp = (ViewFlipper) findViewById(R.id.flipp);
        @Override

        public void onClick(View view) {
            if(Next.getText().equals("Result")){
                finalResult();
                ResultC = 0;
                ResultW = 0;
            }else{flipp.showNext();};
        }
    });

xml文件

<ViewFlipper
    android:id="@+id/flipp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">
<TextView
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:gravity="right"
    android:layout_marginTop="20dp"
    android:layout_marginRight="10dp"
    android:layout_marginLeft="30dp"
    android:textSize="18sp"
    android:text="Q1"/>
    </LinearLayout>

  <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="vertical">
    <TextView
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:gravity="right"
    android:layout_marginTop="20dp"
    android:layout_marginRight="10dp"
    android:layout_marginLeft="30dp"
    android:textSize="18sp"
    android:text="Q2"/>
    </LinearLayout>

   <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="vertical">
    <TextView
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:gravity="right"
    android:layout_marginTop="20dp"
    android:layout_marginRight="10dp"
    android:layout_marginLeft="30dp"
    android:textSize="18sp"
    android:text="Q4"/>
    </LinearLayout>
    </ViewFlipper>
4

3 回答 3

0

you can change the text of the button using button.setText("New label") and in OnclickListener follow this style

if(!last)
{
   //your  usual code for onclick listener
}
else 
{
    // your code for the last view 
}

-----EDITED-----

This is how I advised

boolean last=false;
Button Result = (Button) findViewById(R.id.Button);
Button Next= (Button) findViewById(R.id.Next);
ViewFlipper flipp = (ViewFlipper) findViewById(R.id.flipp);

if(flipp.getDisplayedChild() == flipp.getChildCount()){
   Next.setText("Result");
   last=true;
}
Next.setOnClickListener(new View.OnClickListener() {
    ViewFlipper flipp = (ViewFlipper) findViewById(R.id.flipp);
    @Override

    public void onClick(View view) {
        if(last)){
            finalResult();
            ResultC = 0;
            ResultW = 0;
        }else{flipp.showNext();};
    }
});
于 2013-11-14T08:29:51.670 回答
0

您可以在按下 Next 按钮时检查 .getDisplayedChild() == .getChildCount() 是否。如果它是真的,你已经到达最后一张幻灯片,你可以运行其他命令。

于 2013-11-14T08:32:17.410 回答
0
try this one


if (flipper.indexOfChild(flipper.getCurrentView()) == flipper.getChildCount()){

  button.settext("Start New Quiz");

}

  button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                              if(button.gettext.equals("Start New Quiz")){
                Intent in = new Intent(getApplicationContext(),
                        DashBoardClass.class);

                startActivity(in);

                 }else{

                   //do what you want here 
            }
        });

}
于 2013-11-14T08:41:42.037 回答