Android App - Multiply And Divide The User Entered Values Values In The Two Forms
I'm trying to make an android app, and as I am a total beginner, I am wondering if anyone could help me out with the code. There will be three boxes to enter different numbers, an
Solution 1:
For taking inputs take 3 EditText and take one Button for get Result by Clicking on it.
Follow this
publicclassresultextendsActivity
{
private EditText edit1;
private EditText edit2;
private EditText edit3;
publicvoidonCreate(Bundle savedInstanceState)
{
try
{
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
edit1 = (EditText)findViewById(R.id.edit1);
edit2 = (EditText)findViewById(R.id.edit2);
edit3 = (EditText)findViewById(R.id.edit3);
Buttonclick= (Button)findViewById(R.id.btn);
click.setOnClickListener(newOnClickListener() {
publicvoidonClick(View v) {
inta= Integer.parseInt(edit1.getText().toString());
intb= Integer.parseInt(edit2.getText().toString());
intc= Integer.parseInt(edit3.getText().toString());
doubleresult= ((double) a/b)*c;
Toast.makeText(result.this, Double.toString(result),Toast.LENGTH_LONG).show();
}
});
}catch (Exception e) {
e.printStackTrace();
}
}
}
Result.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="#fff"
><EditTextandroid:id="@+id/edit1"android:layout_width="fill_parent"android:layout_height="40dp"android:inputType="text"/><EditTextandroid:id="@+id/edit2"android:layout_width="fill_parent"android:layout_height="40dp"android:inputType="text"/><EditTextandroid:id="@+id/edit3"android:layout_width="fill_parent"android:layout_height="40dp"android:inputType="text"/><Buttonandroid:id="@+id/btn"android:layout_width="fill_parent"android:layout_height="40dp"android:text="Click"/></LinearLayout>
Solution 2:
Declare 3 EditTexts in your applications layout, as well as a button, and a TextView. Give them unique id's.
The following code will do what you want. It's very simple, so make sure you don't just copy and paste,and that you understand it. I always find it easier to learn from examples, which is why I'm giving one. I hope it helps.
publicclassMainActivityextendsActivity {
//Declare textviews as fields, so they can be accessed throughout the activity.
EditText mEditText1;
EditText mEditText2;
EditText mEditText3;
TextView mTextView;
Button mButton;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Bind the EditText views
mEditText1 = (EditText)findViewById(R.id.editText1);
mEditText2 = (EditText)findViewById(R.id.editText2);
mEditText3 = (EditText)findViewById(R.id.editText3);
mTextView = (TextView)findViewById(R.id.textView1);
mButton = (Button)findViewById(R.id.calculateButton);
mButton.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View v) {
//When the button is clicked, call the calucate method.
calculate();
}
});
}
publicvoidcalculate(){
//get entered texts from the edittexts,and convert to integers.Doublevalue1= Double.parseDouble(mEditText1.getText().toString());
Doublevalue2= Double.parseDouble(mEditText2.getText().toString());
Doublevalue3= Double.parseDouble(mEditText3.getText().toString());
//do the calculationDoublecalculatedValue= (value2/value1)*value3;
//set the value to the textview, to display on screen.
mTextView.setText(calculatedValue.toString());
}
}
Solution 3:
int answer=(Integer.parse(editTextB.getText()) /Integer.parse(editTextA.getText())*Integer.parse(editTextC.getText()
Post a Comment for "Android App - Multiply And Divide The User Entered Values Values In The Two Forms"