package com.example.jsj;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText shuone;
EditText shutwe;
RadioGroup rag;
Button but_five;
TextView jg;
int Result=0;
int shuone1=0;
int shutwe1=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shuone = (EditText) findViewById(R.id.one);
shutwe = (EditText) findViewById(R.id.twe);
rag = (RadioGroup) findViewById(R.id.rag);
jg = (TextView) findViewById(R.id.text_one);
but_five = (Button) findViewById(R.id.but_five);
rag.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
shuone1= Integer.parseInt(shuone.getText().toString().trim());
shutwe1= Integer.parseInt(shutwe.getText().toString().trim());
// TODO Auto-generated method stub
String a ="";
switch (checkedId) {
case R.id.but_one:
a= shuone1+shutwe1+"";
break;
case R.id.but_twe:
a = shuone1-shutwe1+"";
break;
case R.id.but_three:
a= shuone1*shutwe1+"";
break;
case R.id.but_four:
a= shuone1/shutwe1+"";
break;
}
jg.setText(a);
}
});
but_five.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
jg.setText(null);
shuone.setText(null);
shutwe.setText(null);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
![复制代码]()
![复制代码]()
<LinearLayout 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:orientation="vertical"
android:background="#e7e7e7"
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="com.example.jsj.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:text="输入第一个数"
android:layout_marginTop="20dp"/>
<EditText
android:id="@+id/one"
android:layout_width="0dp"
android:layout_height="30dp"
android:background="#ffffff"
android:layout_weight="2"
android:layout_marginTop="20dp">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
>
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:text="输入第二个数"
android:layout_marginTop="20dp"/>
<EditText
android:id="@+id/twe"
android:layout_width="0dp"
android:layout_height="30dp"
android:background="#ffffff"
android:layout_weight="2"
android:layout_marginTop="20dp">
</EditText>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
>
<RadioGroup
android:id="@+id/rag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/but_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="25dp"
android:layout_weight="1"
android:text="+"
/>
<RadioButton
android:id="@+id/but_twe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="25dp"
android:layout_weight="1"
android:text="—"
/>
<RadioButton
android:id="@+id/but_three"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="25dp"
android:layout_weight="1"
android:text="*"
/>
<RadioButton
android:id="@+id/but_four"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="25dp"
android:layout_weight="1"
android:text="/"
/>
</RadioGroup>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<TextView
android:id="@+id/text_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="计算机结果显示在这"
android:layout_centerInParent="true"
/>
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<Button
android:id="@+id/but_five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="清空"/>
</RelativeLayout>
</LinearLayout>