我已经使用这个网站搜索了两个学期的特定错误。我知道我要问的问题比我在本网站上搜索/阅读的大多数内容都更广泛和更基本,但这是我在课堂外唯一要寻求信息的地方。
现在,我正在使用Android Studio开发一个简单的Photo Calculator Phone应用程序。它具有3个单选按钮,每个按钮分配了一个特定值。我知道我在引用每个按钮并尝试触发计算时出了点问题。
当前的一些错误:
Cannot resolve method'getText()'
Operator '<' cannot be applied to 'android.widget.editText','int'
Operator "*" cannot be applied to 'double', 'andriod.widget.EditText'
下面是代码:
package com.example.squirreloverlord.ccarringtonphonephotoprint;
import android.icu.text.DecimalFormat;
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.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
double small = 19;
double medium = 49;
double large = 79;
double Result;
double inputUser;
double Number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
final EditText inputUser = (EditText) findViewById(R.id.editTextNumber);
final RadioButton radioButton4x6 = (RadioButton) findViewById(R.id.radioButton4x6);
final RadioButton radioButton5x7 = (RadioButton) findViewById(R.id.radioButton5x7);
final RadioButton radioButton8x10 = (RadioButton)findViewById(R.id.radioButton8x10);
final TextView Result = (TextView) findViewById(R.id.textViewResult);
Button Calculate = (Button) findViewById(R.id.buttonCalculate);
Calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
inputUser=Double.parseDouble(Number.getText( ).toString( ));
DecimalFormat tenth = new DecimalFormat("#.#");
if(radioButton4x6.isChecked( )) {
if (inputUser <50) {
Number = small * inputUser;
Result.setText(tenth.format(Result) + " is your total cost!");
} else {
Toast.makeText(MainActivity.this,"Please enter a value less than 50", Toast.LENGTH_LONG).show();
}
}
if (radioButton5x7.isChecked( )) {
if (inputUser <50) {
Number = medium * inputUser;
Result.setText(tenth.format(Result) + " is your total cost!");
} else {
Toast.makeText(MainActivity.this,"Please enter a value less than 50", Toast.LENGTH_LONG).show();
}
}
if (radioButton8x10.isChecked()) {
if (inputUser <50) {
Number = large * inputUser;
Result.setText(tenth.format(Result) + " is your total cost!");
} else {
Toast.makeText(MainActivity.this,"Please enter a value less than 50", Toast.LENGTH_LONG).show();
}
}
}
});
}
}
预先感谢您可以提供的任何帮助。
最佳答案
我认为您应该像这样更改变量:
double inputUser;
和
final EditText inputUser = (EditText) findViewById(R.id.editTextNumber);
inputUser将对此代码感到困惑
inputUser=Double.parseDouble(Number.getText( ).toString( ));
如果我是你,我会这样改变:
final EditText editUser= (EditText) findViewById(R.id.editTextNumber);
editUser=Double.parseDouble(editUser.getText( ).toString( ));
然后像代码
editUser <50
一样会得到值希望能帮助到你。
根据您的双印,更改如下:
prints=Double.parseDouble(editUser.getText( ).toString( ));
那么您可以使用if像这样:
if (prints<50)