我想在 android 中计算仪表,但我创建的逻辑不起作用。有什么建议吗?
I want to calculate gauge in android but the logic I created is not working. Any suggestion?
代码如下,我正在尝试制作一个计量计算器,当用户在字段中输入时,计算应根据用户输入的数字执行,新变量应创建并执行一些计算,如下所述。
package com.cyberthreats.activitylifetester.gueagecalculator;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import static com.cyberthreats.activitylifetester.gueagecalculator.R.id.activity_main;
import static com.cyberthreats.activitylifetester.gueagecalculator.R.id.add;
import static com.cyberthreats.activitylifetester.gueagecalculator.R.id.guage1;
import static com.cyberthreats.activitylifetester.gueagecalculator.R.id.weight;
import static com.cyberthreats.activitylifetester.gueagecalculator.R.id.wrap_content;
public class MainActivity extends AppCompatActivity {
EditText guage1, guage2, weight;
Button addmore, calculate;
int value1, value2, totalSum;
float calculated, calculated2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
guage1 = (EditText) findViewById(R.id.guage1);
guage2 = (EditText) findViewById(R.id.guage2);
weight = (EditText) findViewById(R.id.weight);
addmore = (Button) findViewById(R.id.add);
calculate = (Button) findViewById(R.id.calculate);
calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String num1 = guage1.getText().toString();
String num2 = guage2.getText().toString();
String wei = weight.getText().toString();
int number1, number2, number3, weight;
number1 = Integer.parseInt(num1); //int value in number1
number2 = Integer.parseInt(num2); //int value in number2
weight = Integer.parseInt(wei); //int value in weight
//now calculations
switch (number1) {
case 16:
value1 = 4096;
break;
case 17:
value1 = 3136;
break;
case 18:
value1 = 2304;
break;
case 19:
value1 = 1600;
break;
case 20:
value1 = 1296;
break;
case 21:
value1 = 1024;
break;
case 22:
value1 = 784;
break;
case 23:
value1 = 576;
break;
case 24:
value1 = 484;
break;
default:
}
switch (number2) {
case 16:
value2 = 4096;
break;
case 17:
value2 = 3136;
break;
case 18:
value2 = 2304;
break;
case 19:
value2 = 1600;
break;
case 20:
value2 = 1296;
break;
case 21:
value2 = 1024;
break;
case 22:
value2 = 784;
break;
case 23:
value2 = 576;
break;
case 24:
value2 = 484;
break;
}
//switch for number 2 ended
totalSum = value1 + value2;
calculated = value1 / totalSum * weight;
calculated2 = value2 / totalSum * weight;
Intent i = new Intent(MainActivity.this, Main2Activity.class);
i.putExtra("calculated", calculated);
i.putExtra("calculated2", calculated2);
startActivity(i);
}
});
}
}
第二个activity
package com.cyberthreats.activitylifetester.gueagecalculator;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
float calculated, calculated2;
int number3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
TextView r = (TextView) findViewById(R.id.result);
TextView r2 = (TextView) findViewById(R.id.result2);
Intent intent = getIntent();
float i = intent.getFloatExtra("calculated", calculated);
float j = intent.getFloatExtra("calculated", calculated2);
r.setText(String.valueOf(i));
r2.setText(String.valueOf(j));
}
}
以下是您需要更改的内容。
首先将 value1, value2, totalSum;
的类型更改为 float 即 float value1, value2, totalSum;
在 Main2Activity.java
中,编辑以匹配此行
float j = intent.getFloatExtra("calculated2", calculated2);
代码如下,我正在尝试制作一个计量计算器,当用户在字段中输入时,计算应根据用户输入的数字执行,新变量应创建并执行一些计算,如下所述。
package com.cyberthreats.activitylifetester.gueagecalculator;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import static com.cyberthreats.activitylifetester.gueagecalculator.R.id.activity_main;
import static com.cyberthreats.activitylifetester.gueagecalculator.R.id.add;
import static com.cyberthreats.activitylifetester.gueagecalculator.R.id.guage1;
import static com.cyberthreats.activitylifetester.gueagecalculator.R.id.weight;
import static com.cyberthreats.activitylifetester.gueagecalculator.R.id.wrap_content;
public class MainActivity extends AppCompatActivity {
EditText guage1, guage2, weight;
Button addmore, calculate;
int value1, value2, totalSum;
float calculated, calculated2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
guage1 = (EditText) findViewById(R.id.guage1);
guage2 = (EditText) findViewById(R.id.guage2);
weight = (EditText) findViewById(R.id.weight);
addmore = (Button) findViewById(R.id.add);
calculate = (Button) findViewById(R.id.calculate);
calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String num1 = guage1.getText().toString();
String num2 = guage2.getText().toString();
String wei = weight.getText().toString();
int number1, number2, number3, weight;
number1 = Integer.parseInt(num1); //int value in number1
number2 = Integer.parseInt(num2); //int value in number2
weight = Integer.parseInt(wei); //int value in weight
//now calculations
switch (number1) {
case 16:
value1 = 4096;
break;
case 17:
value1 = 3136;
break;
case 18:
value1 = 2304;
break;
case 19:
value1 = 1600;
break;
case 20:
value1 = 1296;
break;
case 21:
value1 = 1024;
break;
case 22:
value1 = 784;
break;
case 23:
value1 = 576;
break;
case 24:
value1 = 484;
break;
default:
}
switch (number2) {
case 16:
value2 = 4096;
break;
case 17:
value2 = 3136;
break;
case 18:
value2 = 2304;
break;
case 19:
value2 = 1600;
break;
case 20:
value2 = 1296;
break;
case 21:
value2 = 1024;
break;
case 22:
value2 = 784;
break;
case 23:
value2 = 576;
break;
case 24:
value2 = 484;
break;
}
//switch for number 2 ended
totalSum = value1 + value2;
calculated = value1 / totalSum * weight;
calculated2 = value2 / totalSum * weight;
Intent i = new Intent(MainActivity.this, Main2Activity.class);
i.putExtra("calculated", calculated);
i.putExtra("calculated2", calculated2);
startActivity(i);
}
});
}
}
第二个activity
package com.cyberthreats.activitylifetester.gueagecalculator;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
float calculated, calculated2;
int number3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
TextView r = (TextView) findViewById(R.id.result);
TextView r2 = (TextView) findViewById(R.id.result2);
Intent intent = getIntent();
float i = intent.getFloatExtra("calculated", calculated);
float j = intent.getFloatExtra("calculated", calculated2);
r.setText(String.valueOf(i));
r2.setText(String.valueOf(j));
}
}
以下是您需要更改的内容。
首先将 value1, value2, totalSum;
的类型更改为 float 即 float value1, value2, totalSum;
在 Main2Activity.java
中,编辑以匹配此行
float j = intent.getFloatExtra("calculated2", calculated2);