Я создаю приложение типа калькулятора, которое возвращает вычисления в набор полей EditText
с помощью функции TextWatcher
. Проблема, с которой я сталкиваюсь, заключается в том, что когда я набираю десятичную дробь перед числом, мое приложение вылетает. Вот что у меня есть до сих пор.
public class MainActivity extends ActionBarActivity {
EditText editText1;
EditText editText2;
EditText editText4;
EditText editText3;
EditText editText5;
EditText editText6;
EditText editText7;
EditText editText8;
TextView textViewtotal;
TextView textViewnet;
TextView textViewproceeds;
TextView textViewprof;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText3);
editText4 = (EditText) findViewById(R.id.editText4);
editText5 = (EditText) findViewById(R.id.editText5);
editText6 = (EditText) findViewById(R.id.editText6);
editText7 = (EditText) findViewById(R.id.editText7);
editText8 = (EditText) findViewById(R.id.editText8);
textViewtotal = (TextView) findViewById(R.id.editText6);
textViewnet = (TextView) findViewById(R.id.editText8);
textViewproceeds = (TextView) findViewById(R.id.editText7);
textViewprof = (TextView) findViewById(R.id.editText9);
editText1.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
textViewproceeds.setText(addProceeds());
textViewtotal.setText(addTotal());
}
});
editText2.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
textViewtotal.setText(addTotal());
}
});
editText3.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
textViewproceeds.setText(addProceeds());
}
});
editText4.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
textViewtotal.setText(addTotal());
}
});
editText5.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
textViewtotal.setText(addTotal());
}
});
editText6.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
textViewnet.setText(addNet());
textViewprof.setText(addProfpercent());
}
});
editText7.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
textViewnet.setText(addNet());
}
});
editText8.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
textViewprof.setText(addProfpercent());
}
});
}
private String addProceeds() {
Double num1;
Double num2;
if(editText1.getText().toString() != "" && editText1.getText().length() > 0) {
num1 = Double.parseDouble(editText1.getText().toString());
} else {
num1 = Double.valueOf(0);
}
if(editText3.getText().toString() != "" && editText3.getText().length() > 0) {
num2 = Double.parseDouble(editText3.getText().toString());
} else {
num2 = Double.valueOf(0);
}
return Double.toString(num1 * num2);
}
private String addTotal() {
Double num1;
Double num2;
Double num3;
Double num4;
if (editText1.getText().toString() != "" && editText1.getText().length() > 0) {
num1 = Double.parseDouble(editText1.getText().toString());
} else {
num1 = Double.valueOf(0);
}
if (editText2.getText().toString() != "" && editText2.getText().length() > 0) {
num2 = Double.parseDouble(editText2.getText().toString());
} else {
num2 = Double.valueOf(0);
}
if (editText4.getText().toString() != "" && editText4.getText().length() > 0) {
num3 = Double.parseDouble(editText4.getText().toString());
} else {
num3 = Double.valueOf(0);
}
if (editText5.getText().toString() != "" && editText5.getText().length() > 0) {
num4 = Double.parseDouble(editText5.getText().toString());
} else {
num4 = Double.valueOf(0);
}
return Double.toString((num1 * num2) + num3 + num4);
}
private String addNet() {
Double num1;
Double num2;
if(editText6.getText().toString() != "" && editText6.getText().length() > 0) {
num1 = Double.parseDouble(editText6.getText().toString());
} else {
num1 = Double.valueOf(0);
}
if(editText7.getText().toString() != "" && editText7.getText().length() > 0) {
num2 = Double.parseDouble(editText7.getText().toString());
} else {
num2 = Double.valueOf(0);
}
return Double.toString(num2 - num1);
}
private String addProfpercent() {
Double num1;
Double num2;
if(editText6.getText().toString() != "" && editText6.getText().length() > 0) {
num1 = Double.parseDouble(editText6.getText().toString());
} else {
num1 = Double.valueOf(0);
}
if(editText8.getText().toString() != "" && editText8.getText().length() > 0) {
num2 = Double.parseDouble(editText8.getText().toString());
} else {
num2 = Double.valueOf(0);
}
return Double.toString((num2 / num1) * 100);
}
Вот xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Number of Shares"
android:id="@+id/textNumShares"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="50dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Initial Share Price"
android:id="@+id/textInitialPrice"
android:layout_below="@+id/textNumShares"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editText1"
android:layout_alignBottom="@+id/textNumShares"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="false"
android:maxLength="8"
android:gravity="right"
android:width="100dp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editText2"
android:layout_alignBottom="@+id/textInitialPrice"
android:layout_alignRight="@+id/editText1"
android:layout_alignEnd="@+id/editText1"
android:maxLength="8"
android:gravity="right"
android:width="100dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Target/Current Share Price"
android:id="@+id/textTargetPrice"
android:layout_below="@+id/textInitialPrice"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editText3"
android:layout_alignBottom="@+id/textTargetPrice"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:maxLength="8"
android:gravity="right"
android:width="100dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Brokerage Buy Fee(s)"
android:id="@+id/textBuyFee"
android:layout_below="@+id/textTargetPrice"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editText4"
android:layout_alignBottom="@+id/textBuyFee"
android:layout_alignRight="@+id/editText3"
android:layout_alignEnd="@+id/editText3"
android:maxLength="5"
android:gravity="right"
android:width="100dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Brokerage Sell Fee(s)"
android:id="@+id/textSellFee"
android:layout_below="@+id/textBuyFee"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editText5"
android:layout_alignBottom="@+id/textSellFee"
android:layout_alignRight="@+id/editText4"
android:layout_alignEnd="@+id/editText4"
android:maxLength="5"
android:gravity="right"
android:width="100dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Total Purchase Cost"
android:id="@+id/textTotalCost"
android:layout_below="@+id/textSellFee"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editText6"
android:layout_alignBottom="@+id/textTotalCost"
android:layout_alignLeft="@+id/editText5"
android:layout_alignStart="@+id/editText5"
android:maxLength="8"
android:gravity="right"
android:width="100dp"
android:editable="false"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:longClickable="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Proceeds"
android:id="@+id/textProceeds"
android:layout_below="@+id/textTotalCost"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editText7"
android:layout_alignBottom="@+id/textProceeds"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:maxLength="8"
android:gravity="right"
android:width="100dp"
android:editable="false"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:longClickable="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Net Profit"
android:id="@+id/textNetProfit"
android:layout_below="@+id/textProceeds"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editText8"
android:layout_alignBottom="@+id/textNetProfit"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:maxLength="8"
android:gravity="right"
android:width="100dp"
android:editable="false"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:longClickable="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Return on Investment"
android:id="@+id/textReturn"
android:layout_below="@+id/textNetProfit"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editText9"
android:layout_alignBottom="@+id/textReturn"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:maxLength="2"
android:gravity="right"
android:width="100dp"
android:editable="false"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:longClickable="false"/>
<EditText
android:layout_width="wrap_content"
android:inputType="numberDecimal"
android:layout_height="wrap_content"
android:id="@+id/editTest1"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="44dp"
android:width="100dp"/>
</RelativeLayout>
Вот логкэт
Спасибо за вашу помощь.
Попробуй это,