创建一个可以执行多个任务的android按钮

时间:2014-04-16 07:47:17

标签: android

我正在制作一个Android应用程序,需要一个按钮来执行两个taks: 1.当用户在EditText中提供错误的输入时,该按钮应在TextView中显示错误消息。

  1. 如果用户提供了正确的输入,那么在点击按钮时,它将跳转到另一个活动。
  2. 我的应用是一种简单的游戏,包含4项活动(activity_1,activity_2,activity_3,activity_4)。 它要求在activity_2中输入。如果输入错误,则会显示错误消息。 但是在提供正确的输入时,应用程序崩溃了。

    代码activity_2.xml:

    <Button
     android:id="@+id/buttonClickMe"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_below="@+id/textView7"
     android:layout_centerHorizontal="true"
     android:layout_marginTop="18dp"
     android:onClick="sendMessage"
     android:text="@string/button_start" />
    

    代码activity_2.java:

    final Button buttonStart = (Button) findViewById(R.id.button1)
    buttonStart.setOnClickListener(new OnClickListener() {
    
            public void onClick(View arg0) {
    
                int no = Integer.valueOf(numberField.getText().toString());
    
                switch (no)
                {
                   case 1 : maxRand = 15;  // the random number will be between 0 and maxRand
                   break;
                   case 2 : maxRand = 30;
                   break;
                   case 3 : maxRand = 50;
                   break;
                   case 4 : maxRand = 100;
                   break;
                   default : textField.setText("Invalid Argument, Please try again.");
                   break;
                }
    
                if(maxRand != 0){
                    randNumber = (int) (Math.random() * maxRand);
                    /*textField.setText("Now click Start button !");
                      buttonStart.setVisibility(View.VISIBLE);*/
                    startActivity(intent);   //This starts a new activity
                }
            }
        });
    

    以及onClick方法后跟的代码:

    public void sendMessage(View view){     //This method is called everytime when the button is clicked
    
        intent = new Intent(this,ShowResultsActivity.class);
    }
    

    我已将意图声明为该类的数据成员。

    请帮帮我。并记得提供一堆代码供参考。

2 个答案:

答案 0 :(得分:1)

尝试在setOnClickListener本身启动Intent

final Button buttonStart =(Button)findViewById(R.id.button1) buttonStart.setOnClickListener(new OnClickListener(){

    public void onClick(View arg0) {

        int no = Integer.valueOf(numberField.getText().toString());

        switch (no)
        {
           case 1 : maxRand = 15;  // the random number will be between 0 and maxRand
           break;
           case 2 : maxRand = 30;
           break;
           case 3 : maxRand = 50;
           break;
           case 4 : maxRand = 100;
           break;
           default : textField.setText("Invalid Argument, Please try again.");
           break;
        }

        if(maxRand != 0){
            randNumber = (int) (Math.random() * maxRand);
            intent = new Intent(this,ShowResultsActivity.class);
            startActivity(intent);   //This starts a new activity
        }
    }
});

如果不能解决问题。在这里看看你的logcat输出..

答案 1 :(得分:0)

public void sendMessage(View view){     
    //This method is called everytime when the button is clicked
    int no = Integer.valueOf(numberField.getText().toString());

            switch (no)
            {
               case 1 : maxRand = 15;  // the random number will be between 0 and maxRand
               break;
               case 2 : maxRand = 30;
               break;
               case 3 : maxRand = 50;
               break;
               case 4 : maxRand = 100;
               break;
               default : 
                   textField.setText("Invalid Argument, Please try again.");
                   // add this to ensure that you won't start activity
                   maxRand = 0;
               break;
            }

            if(maxRand != 0){
                randNumber = (int) (Math.random() * maxRand);
                intent = new Intent(this,ShowResultsActivity.class);
                startActivity(intent);   //This starts a new activity
            } else {
                   // maxRand == 0 ===> ERRORS, update your textField here
                   textField.setText("Invalid Argument, Please try again.");
            }
}