جمع در برنامه نویسی اندروید
چهارشنبه, ۲۹ فروردين ۱۳۹۷، ۰۳:۵۴ ب.ظ
کد جمع دو عدد در برنامه نویسی اندروید
کد xml
<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"
tools:context="com.example.amir.test5.MainActivity">
<TextView
android:layout_width="204dp"
android:layout_height="116dp"
android:hint="Hello World!"
android:textSize="25dp"
android:id="@+id/etext"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="@+id/editText1"
android:background="#ff99ff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="v1"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="134dp"
android:layout_marginLeft="90dp"
app:layout_constraintLeft_toLeftOf="parent" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:background="#ff99ff"
android:inputType="textPersonName"
android:hint="v2"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="193dp"
android:layout_marginLeft="99dp"
app:layout_constraintLeft_toLeftOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_marginLeft="148dp"
app:layout_constraintLeft_toLeftOf="parent"
android:onClick="OnClick"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="231dp"
/>
</android.support.constraint.ConstraintLayout>
روش اول
String intg1 = editText1.getText().toString();
String intg2 = editText2.getText().toString();
int i1 = Integer.parseInt(intg1);
System.out.println(i1);
int i2 = Integer.parseInt(intg2);
System.out.println(i2);
String s = String.valueOf(i1 + i2);
text.setText(s);
روش دوم
String intg1 = editText1.getText().toString();
String intg2 = editText2.getText().toString();
System.out.println(intg1 + "\n" + intg2);
int integer1 = new Integer(intg1);
int integer2 = new Integer(intg2);
System.out.println(integer1 + integer2);//
System.out.println(integer1 + integer2) جمع دو اینت را در android monititor نمایش میدهد
روش سوم
این هم یک نمونه ساده که اولین بار خودم نوشتم
از ادیت تکس ها میگیره و جمع میکنه و در i میریزه
اخر سر هم با متد settext انرا به نمایش در میاورد
int i = Integer.parseInt(editText1.getText().toString()) + Integer.parseInt(editText2.getText().toString());
text.setText(Integer.toString(i));