基础教程第二课
来源:互联网 发布:电子书软件iphone 编辑:程序博客网 时间:2024/05/14 00:35
大家好,今天来讲讲我的第二课,按钮,复选框,什么的。有点乱,不知道从何讲起。就让我们来用一个小例子来学习今天的内容把。
布局文件:2个文本域,3个多选框,一个按钮。代码如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="你的爱好:" /> <CheckBox android:id="@+id/check01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="游泳" /> <CheckBox android:id="@+id/check02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="篮球" /> <CheckBox android:id="@+id/check03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="足球" /> <Button android:id="@+id/button05" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="提交" /> <TextView android:id="@+id/text05" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
主函数如下:
package com.example.android_day02;import android.annotation.SuppressLint;import android.app.Activity;import android.graphics.Color;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.text.Spannable;import android.text.SpannableStringBuilder;import android.text.style.ForegroundColorSpan;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.CheckBox;import android.widget.TextView;public class homework extends Activity implements OnClickListener { TextView tv; CheckBox swimming, basketball, football; Button button; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_homework); this.inital(); } public void inital() { swimming = (CheckBox) findViewById(R.id.check01); basketball = (CheckBox) findViewById(R.id.check02); football = (CheckBox) findViewById(R.id.check03); button = (Button) findViewById(R.id.button05); tv = (TextView) findViewById(R.id.text05); button.setBackgroundColor(Color.BLUE); button.setOnClickListener(this); } public void onClick(View v) { String name = "你的爱好是:"; if (swimming.isChecked()) { name += swimming.getText() + " "; } if (basketball.isChecked()) { name += basketball.getText() + " "; } if (football.isChecked()) { name += football.getText() + " "; } tv.setText(name); SpannableStringBuilder ssb = new SpannableStringBuilder(tv.getText()); ssb.setSpan(new ForegroundColorSpan(Color.RED), 6, name.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE); tv.setText(ssb); }}
这边其实就涉及到一个button的点击事件,非常简单,要是不知道什么是点击事件,左转出门java课程在等你。哈哈~~~为了着重我们选的内容,用到了第一课所学的SpannableStringBilluter。其实我到现在这玩意就用了这几次。我也不知道用啥用。反正先学着,哈哈~。一般“消息(10)”里面的10会用到。算了。不管了。以后用到再说。反正我们都会了~。
下面是效果图:
还有一个小时断电了,大学狗伤不起了。准备玩会睡觉了。。今天就到这把。再见咯!!!
0 0
- JavaScript基础教程第二课
- 基础教程第二课
- Unity3D逆向基础教程第二课
- 第二天 CSS基础教程
- 《新日语基础教程》学习笔记——第二课
- VBS基础教程(第二版)
- Python基础教程—第二章
- oracle基础教程(第二天)
- Python基础教程笔记 第二章
- 《JQuery基础教程》第二章摘记
- python基础教程(第二版)
- Java程序设计基础教程-第二章
- Python基础教程第二版读书笔记-第二章
- 嵌入式linux基础教程第二版 第二章
- ArchieOpenGL基础教程第二课:搭建OpenGL框架② 添加全屏控制
- java程序设计基础教程第二版: 练习P273_exp10_7
- java程序设计基础教程第二版: Server_p315
- java程序设计基础教程第二版:P225_2_Notpad
- 第二题
- 第四章T4
- cout <<string对象 编译出错引发的思考
- 边缘检测后计算区域周长
- 华为oj之递推公式
- 基础教程第二课
- 黑马程序员——关于Java中变量的意义和注意事项
- 第四章T5
- 继承的学习1
- 关于startActivityForResult传值回原activity bundle是null的解决
- 分类改变
- 硕士小论文格式
- oracle表结构
- 网站优化文章写作的思路和流程是什么?