基础教程第二课

来源:互联网 发布:电子书软件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
原创粉丝点击