2
I’m developing an app to multiply two EditText
(quantity X value) and show the result in another EditText
. The problem is that there are several EditText
how the following code shows:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Custos de Implantação"
android:id="@+id/textView110"
android:layout_margin="10dp"
android:textStyle="bold"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:layout_width="@dimen/TamanhoQTDE"
android:layout_height="40dp"
android:inputType="number"
android:ems="10"
android:id="@+id/editQuantidade1"
android:layout_marginTop="@dimen/Formularios"
android:textSize="10dp"
android:textAlignment="center"
android:hint="@string/Quantidade"
android:layout_margin="@dimen/Quantidade"
android:digits="@string/digitosqtde" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="x"
android:id="@+id/textView93"
android:textSize="10dp" />
<EditText
android:layout_width="@dimen/TamanhoQTDE"
android:layout_height="40dp"
android:inputType="number"
android:ems="10"
android:id="@+id/editValor1"
android:layout_marginTop="@dimen/Formularios"
android:textSize="10dp"
android:textAlignment="center"
android:hint="@string/ValorUnitario"
android:layout_margin="@dimen/ValorUnit" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="="
android:id="@+id/textView94"
android:textSize="10dp" />
<EditText
android:layout_width="@dimen/TamanhoQTDE"
android:layout_height="40dp"
android:inputType="number"
android:ems="10"
android:id="@+id/editTotal1"
android:layout_marginTop="@dimen/Formularios"
android:textSize="10dp"
android:textAlignment="center"
android:hint="@string/ValoresTotais"
android:layout_margin="@dimen/ValorTotal"
android:editable="false"
android:focusable="false" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:layout_width="@dimen/TamanhoQTDE"
android:layout_height="40dp"
android:inputType="number"
android:ems="10"
android:id="@+id/editQuantidade2"
android:layout_marginTop="@dimen/Formularios"
android:textSize="10dp"
android:textAlignment="center"
android:hint="@string/Quantidade"
android:layout_margin="@dimen/Quantidade"
android:digits="@string/digitosqtde" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="x"
android:id="@+id/textView99"
android:textSize="10dp" />
<EditText
android:layout_width="@dimen/TamanhoQTDE"
android:layout_height="40dp"
android:inputType="number"
android:ems="10"
android:id="@+id/editValor2"
android:layout_marginTop="@dimen/Formularios"
android:textSize="10dp"
android:textAlignment="center"
android:hint="@string/ValorUnitario"
android:layout_margin="@dimen/ValorUnit" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="="
android:id="@+id/textView100"
android:textSize="10dp" />
<EditText
android:layout_width="@dimen/TamanhoQTDE"
android:layout_height="40dp"
android:inputType="number"
android:ems="10"
android:id="@+id/editTotal2"
android:layout_marginTop="@dimen/Formularios"
android:textSize="10dp"
android:textAlignment="center"
android:hint="@string/ValoresTotais"
android:layout_margin="@dimen/ValorTotal"
android:editable="false"
android:focusable="false" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:layout_width="@dimen/TamanhoQTDE"
android:layout_height="40dp"
android:inputType="number"
android:ems="10"
android:id="@+id/editQuantidade3"
android:layout_marginTop="@dimen/Formularios"
android:textSize="10dp"
android:textAlignment="center"
android:hint="@string/Quantidade"
android:layout_margin="@dimen/Quantidade"
android:digits="@string/digitosqtde" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="x"
android:id="@+id/textView105"
android:textSize="10dp" />
<EditText
android:layout_width="@dimen/TamanhoQTDE"
android:layout_height="40dp"
android:inputType="number"
android:ems="10"
android:id="@+id/editValor3"
android:layout_marginTop="@dimen/Formularios"
android:textSize="10dp"
android:textAlignment="center"
android:hint="@string/ValorUnitario"
android:layout_margin="@dimen/ValorUnit" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="="
android:id="@+id/textView106"
android:textSize="10dp" />
<EditText
android:layout_width="@dimen/TamanhoQTDE"
android:layout_height="40dp"
android:inputType="number"
android:ems="10"
android:id="@+id/editTotal2"
android:layout_marginTop="@dimen/Formularios"
android:textSize="10dp"
android:textAlignment="center"
android:hint="@string/ValoresTotais"
android:layout_margin="@dimen/ValorTotal"
android:editable="false"
android:focusable="false" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="TOTAL:"
android:id="@+id/textView95"
android:textSize="10dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
And below follows the code of Activity
:
package com.seapis.luanrabelo.seapis;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
public class ViveirosBarragem extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viveiros_barragem);
final EditText quantidade1=(EditText)findViewById(R.id.editQuantidade1);
final EditText quantidade2=(EditText)findViewById(R.id.editQuantidade2);
final EditText quantidade3=(EditText)findViewById(R.id.editQuantidade3);
final EditText valor1=(EditText)findViewById(R.id.editValor1);
final EditText valor2=(EditText)findViewById(R.id.editValor2);
final EditText valor3=(EditText)findViewById(R.id.editValor3);
final EditText total1=(EditText)findViewById(R.id.editTotal1);
final EditText total2=(EditText)findViewById(R.id.editTotal2);
final EditText total3=(EditText)findViewById(R.id.editTotal3);
/*Script para esconder o teclado */
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
/*Script para Calculos */
quantidade1.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int quantidade_1 = Integer.parseInt(quantidade1.getText().toString());
int valor_1 = Integer.parseInt(valor1.getText().toString());
int total_1 = (quantidade_1 * valor_1);
total1.setText("R$ " + total_1);
int quantidade_2 = Integer.parseInt(quantidade2.getText().toString());
int valor_2 = Integer.parseInt(valor2.getText().toString());
int total_2 = (quantidade_2 * valor_2);
total2.setText("R$ " + total_2);
int quantidade_3 = Integer.parseInt(quantidade3.getText().toString());
int valor_3 = Integer.parseInt(valor3.getText().toString());
int total_3 = (quantidade_3 * valor_3);
total3.setText("R$ " + total_3);
}
});
}
}
Where the values only calculate when I modify the value of EditText
quantity1
Can someone help me make it better?
So the code goes like this
– Luan Rabelo