Я пытаюсь запустить метод, нажав кнопку. Идея, и, пожалуйста, скажите мне, есть ли лучший способ, поставить весь код для некоторых вычислений в метод Calculateratios. Затем, как только кнопка нажата, она будет запускаться через некоторые операторы if, чтобы проверить и увидеть, пусто ли некоторые текстовые поля. если ни один не пуст, он запускает метод calculateratios. В противном случае отобразите тост, говорящий «Введите все данные». У меня есть следующая java.Как начать метод нажатием кнопки
Java Файл
package com.th3ramr0d.poundforpound;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
Button calculate;
Button reset;
EditText inputname1;
EditText inputname2;
EditText inputbodyweight1;
EditText inputweightlifted1;
EditText inputbodyweight2;
EditText inputweightlifted2;
TextView outputname1;
TextView outputname2;
TextView outputratio1;
TextView outputratio2;
TextView finaloutput;
double inputbodyweight1var = 0;
double inputweightlifted1var = 0;
double inputbodyweight2var = 0;
double inputweightlifted2var = 0;
double ratio1 = 0;
double ratio2 = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calculate = (Button)findViewById(R.id.calculate);
reset = (Button)findViewById(R.id.reset);
inputname1 = (EditText)findViewById(R.id.inputname1);
inputname2 = (EditText)findViewById(R.id.inputname2);
inputbodyweight1 = (EditText)findViewById(R.id.inputbodyweight1);
inputweightlifted1 = (EditText)findViewById(R.id.inputweightlifted1);
inputbodyweight2 = (EditText)findViewById(R.id.inputbodyweight2);
inputweightlifted2 = (EditText)findViewById(R.id.inputweightlifted2);
outputname1 = (TextView)findViewById(R.id.outputname1);
outputname2 = (TextView)findViewById(R.id.outputname2);
outputratio1 = (TextView)findViewById(R.id.outputratio1);
outputratio2 = (TextView)findViewById(R.id.outputratio2);
finaloutput = (TextView)findViewById(R.id.finaloutput);
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
inputname1.setText("");
inputname2.setText("");
inputbodyweight1.setText("");
inputbodyweight2.setText("");
inputweightlifted1.setText("");
inputweightlifted2.setText("");
outputname1.setText("");
outputname2.setText("");
outputratio1.setText("");
outputratio2.setText("");
finaloutput.setText("");
}
}
);
//
//
//ThIS IS WHERE I WANT TO CLICK MY BUTTON TO CHECK IF STATEMENTS
//
//
calculate.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
calculateratios();
}
}
);
}
//THIS IS THE METHOD I WANT TO RUN IF THE IFSTATEMENTS ALLOW IT
public void caculateratios(){
String inputname1var = inputname1.getText().toString();
String inputname2var = inputname2.getText().toString();
inputbodyweight1var = Integer.parseInt(inputbodyweight1.getText().toString());
inputweightlifted1var = Integer.parseInt(inputweightlifted1.getText().toString());
inputbodyweight2var = Integer.parseInt(inputbodyweight2.getText().toString());
inputweightlifted2var = Integer.parseInt(inputweightlifted2.getText().toString());
double ratio1 = inputweightlifted1var/inputbodyweight1var;
double ratio2 = inputweightlifted2var/inputbodyweight2var;
outputname1.setText(inputname1var);
outputname2.setText(inputname2var);
outputratio1.setText(String.format("%.2f" + ": 1", ratio1));
outputratio2.setText(String.format("%.2f" + ": 1", ratio2));
if (ratio1 > ratio2){
double strengthdif = ratio2/ratio1;
strengthdif = (1 - strengthdif) * 100;
finaloutput.setText(inputname1var + " is stronger than " + inputname2var + " by " + (String.format("%.2f", strengthdif)) + "%");
}
else{
double strengthdif = ratio1/ratio2;
strengthdif = (1 - strengthdif) * 100;
finaloutput.setText(inputname2var + " is stronger than " + inputname1var + " by " + (String.format("%.2f", strengthdif)) + "%");
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Проблема заключается в том, что я не уверен в том, как сделать этот метод. Когда я поставил его, как это выше calculateratios() Вызывается в OnClick красный и LogCat говорит:
Ошибка: (87, 21) Ошибка: не удается найти метод символ calculateratios()
Любая помощь о том, как правильно настроить это? Благодаря!
Почему у вас есть; по вашему методу caculateratios? –