运行应用程序时我的按钮似乎不起作用?

问题描述 投票:0回答:1
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash_screen);

    //This will set a 3 second delay before the app opens
    new Handler().postDelayed(new Runnable() {
        public void run() {
            setContentView(R.layout.activity_main);
        }
    }, 5000);
}

char initial;
int space, engineSize;
double miles, rate;
String Combined, fullName; //Global Variables

public void onClick(View view) {

    switch (view.getId()) {

        case R.id.enterEmployeeDetails: //When this button is pressed it will change the layout to the activity_get_name

            setContentView(R.layout.activity_get_name); //When "Enter Employee Details" is pressed it changes to the activity_get_name layout
            //This will show the activity_get_name layout
            break;

        case R.id.switchButton: //When this button is pressed it will store the inputted name

            EditText fName = findViewById(R.id.fullName); //This will store the input value

            String fullName = fName.getText().toString();

            space = fullName.indexOf(" ");
            initial = fullName.charAt(0);
            String last = fullName.substring(space);

            Combined = (initial + last);

            setContentView(R.layout.activity_get_expenses); //This will show the layout activity_get_expenses

            break;

        case R.id.continueProcessing: //When this button is pressed it will change the layout to activity_display

            EditText engSize = findViewById(R.id.engineCcTxt); //This will store the input value
            try { //This will start a try-catch routine
                engineSize = Integer.parseInt(engSize.getText().toString());
            } catch (NumberFormatException e) {
                Toast.makeText(this, "Please enter a number.", Toast.LENGTH_SHORT).show();
                setContentView(R.layout.activity_get_expenses);
                break;
            }

            EditText Distance = findViewById(R.id.miles); //This will store the input value
            try {
                miles = Double.parseDouble(Distance.getText().toString());
            } catch (NumberFormatException e) {

                Toast.makeText(this, "Please enter a number.", Toast.LENGTH_SHORT).show();
                setContentView(R.layout.activity_get_expenses);
                break;
            }

            if (miles < 0) {
                Toast.makeText(this, "Please enter a positive number.", Toast.LENGTH_SHORT).show();
                setContentView(R.layout.activity_get_expenses);
                break;
            }

            double calcuRate = calcRate(engineSize);
            double expDue = calcuRate * miles; //This will workout the Expenses due

            setContentView(R.layout.activity_display); //This will change the layout to activity_display

            TextView empName = findViewById(R.id.displayNameTxt);
            empName.setText(Combined); //This will output the Employee name

            TextView engine = findViewById(R.id.displayEngineTxt);
            engine.setText(Double.toString(engineSize)); //This will output the size of the engine

            TextView milAge = findViewById(R.id.displayMilesTxt);
            milAge.setText(Double.toString(miles)); //This will output the distance travelled

            TextView CalRate = findViewById(R.id.displayRateTxt);
            CalRate.setText("£" + Double.toString(calcuRate)); //This will output the mileage rate

            TextView expen = findViewById(R.id.displayExpensesTxt);
            expen.setText("£" + Double.toString(expDue)); //This will output the Expenses due

            break;

        case R.id.exitButton: //When this button is pressed it will show a splash screen

            setContentView(R.layout.splash_screen);
            //This will set a 3 second delay before the app exits
            new Handler().postDelayed(new Runnable() {
                public void run() {
                    //This will close the app
                    finish();
                }
            }, 5000);
            break;
        case R.id.returnButton: //When this button is pressed it will go back at the start
            setContentView(R.layout.activity_main);
            break;

    }
}

public double calcRate(double size) { //This will calculate the milage rate
    if (size < 1100) {
        rate = 0.64;
    } else if (size <= 2000) {
        rate = 0.72;
    } else rate = 0.79;

    return (rate);
}

该代码适用于大学的计算项目,所以在这方面我是一个完整的代理人。代码应该切换到另一个页面,这样所有的输入都可以填入,然后显示最后的所有数据和计算但是它似乎没有工作,尽管没有错误,并且两个按钮似乎都不起作用。

java android switch-statement
1个回答
0
投票

首先在onCreate()中创建Button的实例并使用setOnClickListener()

Button switchButton = findViewById(R.id.switchButton);
switchButton.setOnClickListener(this);
© www.soinside.com 2019 - 2024. All rights reserved.