简单控件使用--计算BMI的例子

本文通过一个计算BMI的实例展示了Android开发中如何使用XML布局文件创建界面,涉及Button、TextView、RadioButton和EditText等控件的使用。在布局文件`main.xml`中配置各个元素,并在Activity的onCreate()方法内进行关联。为了响应Button点击事件,需让Activity实现OnClickListener接口,并在onClick()方法中添加相应操作。
摘要由CSDN通过智能技术生成

在Android开发中,对于简单的布局和控件使用,可以直接使用xml文件来做布局。

下面用一个简单的计算BMI的例子来说明如何使用Button,TextView这些简单的控件。

布局文件如下: main.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >

<TextView
android:id="@+id/showText"
android:layout_width="wrap_content"
android:layout_height="26px"
android:text="计算你的标准体重!"
android:textSize="25px"
android:layout_x="65px"
android:layout_y="21px">
</TextView>

<TextView
android:id="@+id/text_Sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"
android:layout_x="71px"
android:layout_y="103px">
</TextView>

<TextView
android:id="@+id/text_Height"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="身高:"
android:layout_x="72px"
android:layout_y="169px">
</TextView>

<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="37px"
android:orientation="horizontal"
android:layout_x="124px"
android:layout_y="101px">
<RadioButton
android:id="@+id/Sex_Man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男">
</RadioButton>
<RadioButton
android:id="@+id/Sex_Woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女">
</RadioButton>
</RadioGroup>


<EditText
android:id="@+id/height_Edit"
android:layout_width="123px"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp"
android:layout_x="124px"
android:layout_y="160px">
</EditText>

<Button
android:id="@+id/button_OK"
android:layout_width="80px"
android:layout_height="wrap_content"
android:text="计算"
android:layout_x="125px"
android:layout_y="263px">
</Button>
</AbsoluteLayout>



在这个文件中,定义了3个TextView,2个RadioButton,用于选择性别。一个EditText,用于输入身高。

一个Button,用于计算BMI。

若要使用此布局,则只需要在Activity的onCreate()方法中调用:

setContentView(R.layout.main);


每个控件都有很多内置的属性,其中android:id属性可以用来将Java代码中的控件和此控件联系起来。

Button btn = (Button)findViewById(R.id.button_OK);



对于Button控件,若要为其click事件添加响应函数,可以在Activity中这样做:

1. 让Activity实现OnClickListener这个接口

2. 为Button设置click listener

btn.setOnClickListener(this);

3. 在onClick函数中添加相应的操作

public void onClick(View v) 
{
    switch(v.getId())
    {
         case R.id.button_OK:
         break:
     }
}


完整的Activity的代码如下:

public class BMI extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
	private EditText edit_height;
	private RadioButton radiobutton_Man, radiobutton_Woman;
	private Button btn = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        edit_height = (EditText) findViewById(R.id.height_Edit);
        radiobutton_Man = (RadioButton) findViewById(R.id.Sex_Man);
        radiobutton_Woman = (RadioButton) findViewById(R.id.Sex_Woman);
        
        btn = (Button) findViewById(R.id.button_OK);
        btn.setOnClickListener(this);
        
    }
    
	public void onClick(View v) 
	{
		// TODO Auto-generated method stub
		switch(v.getId())
		{
			case R.id.button_OK:
				try 
				{
					/* 取得输入的身高*/
					double height = Double.parseDouble (edit_height.getText().toString());
					/* 取得选择的性别*/
					String sex = "";
					if (radiobutton_Man.isChecked()) 
					{
						sex = "M";
					} else 
					{
						sex = "F";
					}
					
					
				} 
				catch (Exception e) 
				{
					// TODO: handle exception
					Toast.makeText(this,"error", Toast.LENGTH_LONG).show();
				}
				break;
		}
			
	}
}


运行结果如图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值