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

本文通过实例展示了Android UI中TextView、EditText、Button和RadioButton的基本使用,包括布局XML文件的修改和Activity的创建,帮助开发者熟悉这些组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、代码工程

      本例子在前面(二)中的工程进行改造,目的是熟悉Android UI的基本常用组件TextView、EditText、Button、RadioButton等。

 

二、修改main.xml 布局,添加UI 元素

<?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>

 

三、新建mylayout.xml,并添加UI 元素

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

<TextView
android:id="@+id/text1"

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_x="50px"
android:layout_y="72px"
></TextView>
</AbsoluteLayout>

 

四、新建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等最基本常用组件的用法,例子简单,但足以熟悉这几个组件的用法。

1、Android显示GIF动画 GifView GifView 是一个为了解决android中现在没有直接显示gif的view,只能通过mediaplay来显示这个问题的项目,其用法和 ImageView一样,支持gif图片 使用方法:1-把GifView.jar加入你的项目。2-在xml中配置GifView的基本属性,GifView继承自View类,和Button、ImageView一样是一个UI控件。 如: 3-在代码中配置常用属性: // 从xml中得到GifView的句柄 gf1 = (GifView) findViewById(R.id.gif1); // 设置Gif图片源 gf1.setGifImage(R.drawable.gif1); // 添加监听器 gf1.setOnClickListener(this); // 设置显示的大小,拉伸或者压缩 gf1.setShowDimension(300, 300); // 设置加载方式:先加载后显示、边加载边显示、只显示第一帧再显示 gf1.setGifImageType(GifImageType.COVER); GifView的Jar包共有四个类: GifAction.java 观察者类,监视GIF是否加载成功 GifFrame.java 里面个成员:当前图片、延时、下张Frame的链接。 GifDecoder.java 解码线程类 GifView.java 主类,包括常用方法,如GifView构造方法、设置图片源、延迟、绘制等。 2、Calendar.v0.5.0 是 Android 平台的一个日历显示组件。 3、CWAC EndlessAdapter 是 Android 上一个可以无限往下滑进行列表数据加载的控件。 4、Android Horizontal ListView 是 Android 上一个水平滑动的 ListView 组件。 5、Android ViewBadger 视图布局。 6、滑动刷新的ListView Android PullToRefresh 为 Android 应用提供一个向下滑动即刷新列表的功能,就两个目标文件。 7、pakerfeldt-android-viewflow 是 Android 平台上一个视图切换的效果库。ViewFlow 相当于 Android UI 部件提供水平滚动的 ViewGroup,使用 Adapter 进行条目绑定。 8、Android 导航菜单 RibbonMenu 是 Android 上的一个导航菜单组件。就个目标文件,菜单项直接在 XML 中定义,可添加文本和图标。 9、AndroidUI工具包 android-ui-utils 是一个工具包用来帮助设计和开发 Android 用户界面,包含个单独的工具:Android Asset Studio用户界面原型模具,Android 设计预览,时常需要重复确认程序版面设计状况的 Android App 开发者,应该会爱上这个轻量级的 Java 程序:Andorid Design Preview 工具,通过 USB 连接之后,只要简单的在计算机中选取您想要显示的程序版面范围,就可将镜像结果直接显示于手机装置之上。 10、Androidui开发类库 GreenDroid 是一个Androidui开发类库,能够使你的Android开发更加简便和快捷。 11、Android滑动式菜单 SlidingMenu 是 Android 上实现类似 Facebook 和 Path 2.0 滑动式菜单的组件。 12、AsyncImageView 是 Android 上的一个异步从网络上获取图片并进行浏览的开源组件,可自动在本地进行缓存。该项目是 GreenDroid 的一部分。 13、仿Path按钮动画效果 PathButton 仿照Path应用首页左下角的Button动画效果写了个简单的Demo,由于数学不好,坐标总是和理想有出入,只是大致实现了动画效果,若果有人能把坐标算对,那么修改我的demo就能做成类似Path的那种动画效果!希望大家出点力帮着优化一下,并分享出来! 14、Android Intent开发包 OpenIntents Ope
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值