Andriod编程基础(三):Android UI 基本常用组件实例

  一、代码工程
  本例子在前面(二)中的工程进行改造,目的是熟悉Android UI的基本常用组件TextView、EditText、Button、RadioButton等。
  二、修改main.xml 布局,添加UI 元素
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  三、新建mylayout.xml,并添加UI 元素
  
  
  
  
  四、新建mylayout.java,并在在AndroidManifest.xml 添加Activity 定义
  public class mylayout extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  /* 加载main.xml Layout */
  setContentView(R.layout.mylayout);
  /* 取得Intent 中的Bundle 对象*/
  Bundle bunde = this.getIntent().getExtras();
  /* 取得Bundle 对象中的数据*/
  String sex = bunde.getString("sex");
  double height = bunde.getDouble("height");
  /* 判断性别*/
  String sexText = "";
  if (sex.equals("M")) {
  sexText = "男性";
  } else {
  sexText = "女性";
  }
  String weight = this.getWeight(sex, height);
  /* 设置输出文字*/
  TextView tv1 = (TextView) findViewById(R.id.text1);
  tv1.setText("你是一位" + sexText + "\n你的身高是" + height +
  "厘米\n你的标准体重是"+ weight + "公斤");
  }
  /* 四舍五入的method */
  private String format(double num) {
  NumberFormat formatter = new DecimalFormat("0.00");
  String s = formatter.format(num);
  return s;
  }
  /* 以findViewById()取得Button 对象,并添加onClickListener */
  private String getWeight(String sex, double height) {
  String weight = "";
  if (sex.equals("M")) {
  weight = format((height - 80) * 0.7);
  } else {
  weight = format((height - 70) * 0.6);
  } return weight;
  }
  }
  五、修改原工程中mytest.java的代码
  public class Ex10_UI extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  /* 载入main.xml Layout */
  setContentView(R.layout.main);
  /* 以findViewById()取得Button 对象,并添加onClickListener */
  Button ok = (Button) findViewById(R.id.button_OK);
  ok.setOnClickListener(new Button.OnClickListener() {
  public void onClick(View v) {
  /* 取得输入的身高*/
  EditText et = (EditText) findViewById(R.id.height_Edit);
  double height = Double.parseDouble(et.getText().toString());
  /* 取得选择的性别*/
  String sex = "";
  RadioButton rb1 = (RadioButton) findViewById(R.id.Sex_Man);
  if (rb1.isChecked()) {
  sex = "M";
  } else {
  sex = "F";
  }
  Intent intent = new Intent();
  // new一 个 Intent对象并指定class
  intent.setClass(mytest.this, mylayout.class);
  /* new 一个Bundle对象,并将要传递的数据传入*/
  Bundle bundle = new Bundle();
  bundle.putDouble("height", height);
  bundle.putString("sex", sex);
  /* 将Bundle 对象assign 给Intent */
  intent.putExtras(bundle);
  /* 调用Activity EX03_10_1 */
  startActivity(intent);
  }
  });
  }
  }
  六、运行结果
  
  点击桌面上Mytest图标(即本例子的应用),运行主程序代码,跳到以下页面。
  
  选择性别,输入身高,并点击【计算】,则跳到以下结果页面。
  
  在本例子中,主要是介绍到了TextView、EditText、Button、RadioButton等最基本常用组件的用法,例子简单,但足以熟悉这几个组件的用法。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值