@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);
}
该代码用于大学的一个计算项目,因此在这方面我是一个完整的专家。该代码应该切换到另一页,以便可以填写所有输入,然后在末尾显示所有数据和计算,但是,尽管没有错误,并且似乎也没有任何作用,但它似乎不起作用。 / strong>
答案 0 :(得分:0)
首先在onCreate()中创建您的Button实例,然后使用setOnClickListener()
Button switchButton = findViewById(R.id.switchButton);
switchButton.setOnClickListener(this);