Java 无法识别EditText变量的值(appcompat错误)并成功转换编辑文本
我目前正在尝试找出如何查看编辑文本变量的值。在我的代码中,我试图获取一个用户输入的数字,并将数学应用于它,从而生成一个tip计算器。唯一的问题是,如果用户没有为编辑文本变量输入任何要初始化的值,则编辑文本变量的值不是null,甚至不是空字符串,而是此处列出的一行非常长的代码: android.support.v7.widget.AppCompatiEditText{1b372dd VFED..CL..I.0,0-0,0#7f07009b应用程序:id/userInput} 这是名为userInputMount的编辑文本变量的值,该变量是使用带有参数(R.id.userInput)的findViewById方法初始化的 但是,当我尝试使用double.parseDouble方法和参数(userInputMount.getText().toString())将编辑文本变量的值userInputMount绑定到双精度值userAmount时,会发生错误 我需要实际识别EditText的值的原因是,我可以创建一个绕过双重转换的逻辑门,这样它就不会使我的应用程序崩溃 我想做的是Java 无法识别EditText变量的值(appcompat错误)并成功转换编辑文本,java,android,android-edittext,android-appcompat,Java,Android,Android Edittext,Android Appcompat,我目前正在尝试找出如何查看编辑文本变量的值。在我的代码中,我试图获取一个用户输入的数字,并将数学应用于它,从而生成一个tip计算器。唯一的问题是,如果用户没有为编辑文本变量输入任何要初始化的值,则编辑文本变量的值不是null,甚至不是空字符串,而是此处列出的一行非常长的代码: android.support.v7.widget.AppCompatiEditText{1b372dd VFED..CL..I.0,0-0,0#7f07009b应用程序:id/userInput} 这是名为userInp
if(editTextVariable != null && editTextVariable != "")
//Do the double conversion
else
//set the double variable equal to 0 so it wont crash the app.
TLDR:如果值是android.support.v7.widget.appcompatiedittext{1b372dd VFED..CL....I.0,0-0,0#7f07009b app:id/userInput}我也不希望将编辑文本变量初始化为该值。我想在初始化之前用if语句检查xml引用(userInput)的值,但我不知道怎么做
此语句出现错误
userAmount = Double.parseDouble(userInputAmount.getText().toString());
下面是代码和xml
package com.example.frankbuddy.advancedtipcalculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.graphics.Color;
public class MainActivity extends AppCompatActivity {
SeekBar tipSeekBar;
int seekBarValue;
//For custom tip percentage ($%)
TextView tipDisplay;
//For applied custom percentage to user input (#.#$)
TextView tipAmountDisplay;
//For 15% tip
TextView defTipAmount;
//For Default 15% tip total amount
TextView defTotalAmount;
//For Custom % tip total amount
TextView mainTotalAmount;
//For user input amount
EditText userInputAmount;
double userAmount;
double defaultTip;
double defaultTotal;
double userTotalTip;
double userCustomTotal;
private final int ORANGE = 0xFFFF3300;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialize custom percentage display amount
tipDisplay = (TextView) findViewById(R.id.tipDisplayPercentage);
//Intialize user input amount edit text
userInputAmount =(EditText)findViewById(R.id.userInput);
//Initialize tip amount display
tipAmountDisplay = (TextView)findViewById(R.id.tipDisplayAmount);
//Initialize default tip amount
defTipAmount = (TextView)findViewById(R.id.defaultTipAmount);
//Initialize default total amount
defTotalAmount = (TextView)findViewById(R.id.defaultTotalAmount);
//Initialize custom total amount
mainTotalAmount = (TextView)findViewById(R.id.customTotalAmount);
userTotalTip = 0.0;
userCustomTotal = 0.0;
defaultTotal = 0;
defaultTip = 0;
userAmount = 0;
//Seek Bar initialize
tipSeekBar = (SeekBar)findViewById(R.id.seekBar);
tipSeekBar.setMax(100);
tipSeekBar.setProgress(15);
//Display initial custom tip amount (Default 15%)
tipDisplay.setTextColor(Color.GREEN);
tipDisplay.setText(tipSeekBar.getProgress() + "%");
//Seek bar listener
tipSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
//initialize progress value (To be used for display and math)
int progressChangedValue = 0;
//Realtime listener for updates
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
//Set progress changed value
progressChangedValue = progress ;
//Update custom display percentage amount
tipDisplay.setText(progressChangedValue + "%");
if(progressChangedValue <= 20)
{
tipDisplay.setTextColor(Color.GREEN);
}
if(progressChangedValue >= 21 && progressChangedValue <= 29)
{
tipDisplay.setTextColor(Color.YELLOW);
}
//Display warning if progress value meets or exceeds 30%
if(progressChangedValue >= 30)
{
Toast.makeText(MainActivity.this, "Warning, Tip is now exceeding 30% and the value is now " + progressChangedValue + "%",
Toast.LENGTH_SHORT).show();
tipDisplay.setTextColor(Color.RED);
}
//Bind user input amount into a double variable to apply mathematics
userAmount = Double.parseDouble(userInputAmount.getText().toString());
//Do math for custom tip amount
userTotalTip = userAmount * progressChangedValue *.01;
tipAmountDisplay.setText(String.format("%.2f", userTotalTip));
//Do math for custom total amount
userCustomTotal = userTotalTip + userAmount;
mainTotalAmount.setText(String.format("%.2f", userCustomTotal));
//Do math for default tip amount
defaultTip = userAmount * .15;
defTipAmount.setText(String.format("%.2f", defaultTip));
//Do math for default total amount
defaultTotal = defaultTip + userAmount;
defTotalAmount.setText(String.format("%.2f",defaultTotal ));
}
//Listener for first changes
public void onStartTrackingTouch(SeekBar seekBar)
{
}
//Listener for when the user stops manipulating the bar
public void onStopTrackingTouch(SeekBar seekBar)
{
}
});
}
package com.example.frankbuddy.advancedtipcalculator;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.widget.EditText;
导入android.widget.SeekBar;
导入android.widget.TextView;
导入android.widget.Toast;
导入android.graphics.Color;
公共类MainActivity扩展了AppCompatActivity{
希克巴-提普希克巴;
int seekBarValue;
//自定义小费百分比($%)
文本视图显示;
//用于将自定义百分比应用于用户输入(#.#$)
文本视图显示;
//15%的小费
文本视图defTipAmount;
//对于默认的15%小费总额
TextView defTotalAmount;
//对于自定义的%小费总额
TextView mainTotalAmount;
//对于用户输入量
EditText用户输入量;
双倍用户数量;
双尖端;
双倍违约总额;
双用户终端;
双倍总收入;
专用最终int橙色=0xFFFF3300;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化自定义百分比显示量
tipDisplay=(TextView)findViewById(R.id.tipDisplayPercentage);
//初始化用户输入量编辑文本
UserInputMount=(EditText)findViewById(R.id.userInput);
//初始化小费金额显示
tipAmountDisplay=(TextView)findViewById(R.id.tipDisplayAmount);
//初始化默认小费金额
defTipAmount=(TextView)findViewById(R.id.defaultTipAmount);
//初始化默认总金额
defTotalAmount=(TextView)findViewById(R.id.defaultTotalAmount);
//初始化自定义总金额
mainTotalAmount=(TextView)findViewById(R.id.customTotalAmount);
userTotalTip=0.0;
userCustomTotal=0.0;
defaultTotal=0;
defaultTip=0;
userAmount=0;
//搜索栏初始化
tipSeekBar=(SeekBar)findViewById(R.id.SeekBar);
tipSeekBar.setMax(100);
tipSeekBar.setProgress(15);
//显示初始自定义小费金额(默认值15%)
tipDisplay.setTextColor(Color.GREEN);
tipDisplay.setText(tipSeekBar.getProgress()+“%”);
//搜索栏侦听器
tipSeekBar.setonseekbarchaneglistener(新SeekBar.onseekbarchaneglistener(){
//初始化进度值(用于显示和计算)
int progressChangedValue=0;
//更新的实时侦听器
public void onProgressChanged(SeekBar-SeekBar、int-progress、boolean-fromUser)
{
//设置进度更改值
progressChangedValue=进度;
//更新自定义显示百分比金额
tipDisplay.setText(progressChangedValue+“%”);
if(progressChangedValue=21&&progressChangedValue=30)
{
Toast.makeText(MainActivity.this,“警告,提示现在超过30%,值现在为“+progressChangedValue+”%”,
吐司。长度(短)。show();
tipDisplay.setTextColor(Color.RED);
}
//将用户输入量绑定到双变量中以应用数学
userAmount=Double.parseDouble(userInputMount.getText().toString());
//计算自定义小费金额
userTotalTip=userAmount*progressChangedValue*.01;
tipAmountDisplay.setText(String.format(“%.2f”,userTotalTip));
//计算自定义总金额
userCustomTotal=userTotalTip+userAmount;
maintottalamount.setText(String.format(“%.2f”,userCustomTotal));
//计算默认小费金额
defaultTip=用户金额*.15;
defTipAmount.setText(String.format(“%.2f”,defaultTip));
//计算默认总金额
defaultTotal=defaultTip+userAmount;
defTotalAmount.setText(String.format(“%.2f”,defaultTotal));
}
//第一次更改的侦听器
开始跟踪触摸时的公共无效(SeekBar SeekBar)
{
}
//当用户停止操作栏时的侦听器
TopTrackingTouch(SeekBar SeekBar)上的公共无效
{
}
});
}
和XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
tools:context=".MainActivity">
<TextView
android:id="@+id/tipDisplayAmount"
android:layout_width="76dp"
android:layout_height="31dp"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="8dp"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.555"
app:layout_constraintStart_toEndOf="@+id/defaultTipAmount"
app:layout_constraintTop_toBottomOf="@+id/tipDisplayPercentage" />
<TextView
android:id="@+id/textView4"
android:layout_width="123dp"
android:layout_height="31dp"
android:layout_marginStart="20dp"
android:layout_marginTop="24dp"
android:layout_marginBottom="8dp"
android:text="Total"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3"
app:layout_constraintVertical_bias="0.011" />
<TextView
android:id="@+id/textView"
android:layout_width="125dp"
android:layout_height="35dp"
android:layout_marginStart="8dp"
android:layout_marginTop="120dp"
android:layout_marginEnd="8dp"
android:text="Amount $"
android:textAlignment="center"
android:textSize="26sp"
app:layout_constraintEnd_toStartOf="@+id/userInput"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="127dp"
android:layout_height="35dp"
android:layout_marginStart="16dp"
android:layout_marginTop="28dp"
android:text="Custom %"
android:textAlignment="center"
android:textSize="26sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<EditText
android:id="@+id/userInput"
android:layout_width="207dp"
android:layout_height="42dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="112dp"
android:ems="10"
android:hint="0.00"
android:inputType="numberDecimal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="209dp"
android:layout_height="35dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="8dp"
android:layout_marginTop="28dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/userInput" />
<TextView
android:id="@+id/textView3"
android:layout_width="123dp"
android:layout_height="31dp"
android:layout_marginStart="16dp"
android:layout_marginTop="56dp"
android:text="Tip"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/textView5"
android:layout_width="76dp"
android:layout_height="31dp"
android:layout_marginStart="168dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="15%"
android:textAlignment="center"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/textView3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2"
app:layout_constraintVertical_bias="0.666" />
<TextView
android:id="@+id/tipDisplayPercentage"
android:layout_width="76dp"
android:layout_height="31dp"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="8dp"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.583"
app:layout_constraintStart_toEndOf="@+id/textView5"
app:layout_constraintTop_toBottomOf="@+id/seekBar" />
<TextView
android:id="@+id/defaultTipAmount"
android:layout_width="76dp"
android:layout_height="31dp"
android:layout_marginStart="32dp"
android:layout_marginTop="12dp"
android:textAlignment="center"
android:textSize="18sp"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/textView5" />
<TextView
android:id="@+id/defaultTotalAmount"
android:layout_width="73dp"
android:layout_height="31dp"
android:layout_marginStart="28dp"
android:layout_marginTop="24dp"
android:layout_marginBottom="8dp"
android:textAlignment="center"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView4"
app:layout_constraintTop_toBottomOf="@+id/defaultTipAmount"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/customTotalAmount"
android:layout_width="76dp"
android:layout_height="31dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="28dp"
android:layout_marginBottom="8dp"
android:textAlignment="center"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.535"
app:layout_constraintStart_toEndOf="@+id/defaultTotalAmount"
app:layout_constraintTop_toBottomOf="@+id/tipDisplayAmount"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
T
userAmount = Double.parseDouble(userInputAmount.getText().toString());
Editable text = userInputAmount.getText();
if (text == null || text.toString.length < 1) userAmount = 0; //or whatever you want as the default value
else userAmount = Double.parseDouble(text.toString());