سلام
اقا این کدهاش چرا اینقدر زیاده کلی شرط براش گذاشتی
من قبلا یه ماشین حساب نوشته بودم که کدهاش کمتره میزارم اگه دوست داشتی از این کد کمک بگیر
کدهای جاوا
کد پیاچپی:
package ir.kando.mashinhesab;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText n1 = (EditText) findViewById(R.id.editText1);
final EditText n2 = (EditText) findViewById(R.id.editText2);
final TextView an = (TextView) findViewById(R.id.answer);
Button mosbat = (Button) findViewById(R.id.jam);
Button manfi = (Button) findViewById(R.id.kam);
Button zarb = (Button) findViewById(R.id.zarb);
Button taghsim = (Button) findViewById(R.id.taghsim);
mosbat.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int e1 =Integer.parseInt(n1.getText().toString());
int e2 = Integer.parseInt(n2.getText().toString());
int e3 = e1 + e2;
an.setText("javab"+e1+"+"+e2+"="+e3);
}
});
manfi.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int e1 =Integer.parseInt(n1.getText().toString());
int e2 = Integer.parseInt(n2.getText().toString());
int e3 = e1 - e2;
an.setText("javab"+e1+"-"+e2+"="+e3);
}
});
zarb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int e1 =Integer.parseInt(n1.getText().toString());
int e2 = Integer.parseInt(n2.getText().toString());
int e3 = e1 * e2;
an.setText("javab"+e1+"*"+e2+"="+e3);
}
});
taghsim.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int e1 =Integer.parseInt(n1.getText().toString());
int e2 = Integer.parseInt(n2.getText().toString());
if(e2 != 0){
int e3 = e1 / e2;
an.setText("javab"+e1+"/"+e2+"="+e3);
}
else{
an.setText("تقسیم بر صفر امکان ندارد!");
}
}
});
}
}
کدهای 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="29dp"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginLeft="14dp"
android:layout_marginTop="42dp"
android:ems="10"
android:inputType="number" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Number 1:" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="21dp"
android:text="Number 2:" />
<Button
android:id="@+id/kam"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/zarb"
android:layout_below="@+id/jam"
android:background="#FF4848"
android:text="-" />
<Button
android:id="@+id/zarb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/jam"
android:layout_below="@+id/kam"
android:background="#A55FEB"
android:text="*" />
<Button
android:id="@+id/taghsim"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/zarb"
android:layout_below="@+id/zarb"
android:background="#48FB0D"
android:text="/" />
<Button
android:id="@+id/jam"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/answer"
android:background="#06DCFB"
android:text="+" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/textView2"
android:text="lماشین حساب"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/editText2"
android:layout_marginLeft="40dp"
android:layout_marginTop="26dp"
android:text="جواب" />
</RelativeLayout>
اینم کدهای manifest
کد پیاچپی:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ir.kando.mashinhesab"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="1"
android:targetSdkVersion="19" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="ir.kando.mashinhesab.Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="ir.kando.mashinhesab.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="ir.kando.mashinhesab.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
یه توضیح کوچیک بدم که من تو کدهای خودم اومدم از تابع زیر استفاده کردم و مشکل تقسیم بر صفر رو با نمایش یک متن به کاربر حل کردم که پیغام میده که میگه تقسیم بر صفر امکان نداره
کد پیاچپی:
taghsim.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int e1 =Integer.parseInt(n1.getText().toString());
int e2 = Integer.parseInt(n2.getText().toString());
if(e2 != 0){
int e3 = e1 / e2;
an.setText("javab"+e1+"/"+e2+"="+e3);
}
else{
an.setText("تقسیم بر صفر امکان ندارد!");
}
}
});
امیدوارم کمکت کنه این کدها
موفق باشی