Android布局

[img]http://dl.iteye.com/upload/attachment/0084/0034/e5e77136-3c7a-3af2-a8f7-257d5fce5289.png[/img]
//页面布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="数字1"
android:layout_marginLeft="10sp"
android:layout_marginTop="10sp"
/>

<EditText
android:id="@+id/num1"
android:inputType="number"
android:layout_width="240sp"
android:layout_height="wrap_content"
android:layout_marginLeft="10sp"
android:layout_marginTop="10sp"

/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="数字2"
android:layout_marginLeft="10sp"
android:layout_marginTop="20sp"
/>

<EditText
android:id="@+id/num2"
android:inputType="number"
android:layout_width="240sp"
android:layout_height="wrap_content"
android:layout_marginLeft="10sp"
android:layout_marginTop="15sp"

/>

<RadioGroup
android:id="@+id/rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5sp"
android:layout_marginLeft="10sp"
>

<RadioButton
android:id="@+id/jia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加"
/>

<RadioButton
android:id="@+id/jian"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="减"
/>

<RadioButton
android:id="@+id/cheng"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="乘"
/>

<RadioButton
android:id="@+id/chu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="除"
/>

</RadioGroup>

<Button
android:id="@+id/btncomput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计算"
android:textSize="12sp"
android:layout_marginLeft="10sp"
android:layout_marginTop="10sp"
/>


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计算结果"
android:layout_marginLeft="10sp"
android:layout_marginTop="15sp"
/>

<EditText
android:id="@+id/result"
android:inputType="number"
android:layout_width="240sp"
android:layout_height="wrap_content"
android:layout_marginLeft="10sp"
android:layout_marginTop="10sp"
/>



</LinearLayout>


//加页面代码
public class MainActivity extends Activity {
//定义变量
private EditText et1,et2,etRes;
private Button btn;
int results;
private RadioGroup group;
private RadioButton r1,r2,r3,r4;

private int num1;
private int num2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//获得ID
et1 = (EditText) findViewById(R.id.num1);
et2 = (EditText) findViewById(R.id.num2);
etRes = (EditText) findViewById(R.id.result);
btn = (Button) findViewById(R.id.btncomput);


group = (RadioGroup) findViewById(R.id.rg);
r1 = (RadioButton) findViewById(R.id.jia);
r2 = (RadioButton) findViewById(R.id.jian);
r3 = (RadioButton) findViewById(R.id.cheng);
r4 = (RadioButton) findViewById(R.id.chu);



group.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
num1 = Integer.parseInt(et1.getText().toString());
num2 = Integer.parseInt(et2.getText().toString());

switch(checkedId)
{
case R.id.jia:
results = num1+num2;
break;
case R.id.jian:
results = num1-num2;
break;
case R.id.cheng:
results = num1*num2;
break;
case R.id.chu:
results = num1/num2;
break;
}



}
});


//监听Button按钮
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//btn = (Button) v;
//int results = Integer.parseInt(etRes.getText().toString());
etRes.setText(results);
Toast.makeText(MainActivity.this, etRes.getText().toString(), Toast.LENGTH_LONG).show();
//Toast.makeText(MainActivity.this, etRes.setText(results), Toast.LENGTH_LONG).show();
}
} );




}
[img]http://dl.iteye.com/upload/attachment/0084/0034/e5e77136-3c7a-3af2-a8f7-257d5fce5289.png[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值