功能:计算器的加减乘除
平台:AStudio Studio 3.0.1
直接上代码:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.lenovo.design4application"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" /> <activity android:name=".musicActivity"> </activity> > <activity android:name=".WelcomeActivity" /> <activity android:name=".A_Activity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".B_Activity"></activity> </application> </manifest>
activity_a_.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="输入a=" android:textSize="24sp" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="textPersonName" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="输入b=" android:textSize="24sp" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="textPersonName" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <RadioGroup android:id="@+id/math" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="horizontal" android:visibility="visible"> <RadioButton android:id="@+id/radioButton4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="/" /> <RadioButton android:id="@+id/radioButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="*" /> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="+" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="-" /> </RadioGroup> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:layout_weight="1" android:orientation="horizontal"> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_weight="1" /> </LinearLayout> </LinearLayout>
A_activity.java
package com.example.lenovo.design4application; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; public class A_Activity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_a_); RadioGroup rg=(RadioGroup)findViewById(R.id.math); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton rb=(RadioButton)findViewById(checkedId); EditText et1=(EditText)findViewById(R.id.editText1); EditText et2=(EditText)findViewById(R.id.editText2); String a=et1.getText().toString(); String b=et2.getText().toString(); String c=rb.getText().toString(); Intent intent=new Intent(A_Activity.this,B_Activity.class); Bundle bundle=new Bundle(); bundle.putString("a", a); bundle.putString("b", b); bundle.putString("c", c); intent.putExtras(bundle); startActivityForResult(intent, 100); //关键语句 } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==100) if(resultCode==RESULT_OK){ Bundle bundle=data.getExtras(); int s=bundle.getInt("sum"); // double s=bundle.getDouble("sum"); // String s=bundle.getString("sum"); TextView tv=(TextView)findViewById(R.id.textView3); tv.setText("结果="+s); } if(resultCode==2){ Bundle bundle=data.getExtras(); // int s=bundle.getInt("sum"); double s=bundle.getDouble("sum"); //s=bundle.getString("sum"); TextView tv=(TextView)findViewById(R.id.textView3); tv.setText("结果="+s); } } }
B_activity.java
package com.example.lenovo.design4application; import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class B_Activity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_b_); Intent intent=getIntent(); Bundle bundle=intent.getExtras(); String a=bundle.getString("a"); String b=bundle.getString("b"); String c=bundle.getString("c"); if(c.equals("-")) { int sum=Integer.parseInt(a)-Integer.parseInt(b); bundle.putInt("sum", sum); //换成int数据 intent.putExtras(bundle); setResult(RESULT_OK, intent); //关键语句 finish(); } if(c.equals("+")) { int sum = Integer.parseInt(a) + Integer.parseInt(b); bundle.putInt("sum", sum); //换成int数据 intent.putExtras(bundle); setResult(RESULT_OK, intent); //关键语句 finish(); } if(c.equals("*")) { int sum = Integer.parseInt(a) * Integer.parseInt(b); bundle.putInt("sum", sum); //换成int数据 intent.putExtras(bundle); setResult(RESULT_OK, intent); //关键语句 finish(); } if(c.equals("/")) { double m= Double.parseDouble(a); double n= Double.parseDouble (b); double sum =m *1.0/ n; bundle.putDouble("sum", sum); //换成double数据 intent.putExtras(bundle); setResult(2, intent); //关键语句 finish(); } } }//记得建一个空白的activity_b_
效果见截图:
本人在自己android 6.0手机上运行没问题,建议在自己手机上运行
如果本博客对你有帮助,希望点赞加关注