Android学习第2步--Activity组件

来源:互联网 发布:mac远程连接windows 7 编辑:程序博客网 时间:2024/05/21 19:41

按钮 Button

文本框 EditText

标签 TextView

单选 RadioButton(ischeck)

复选 CheckBox(ischeck)

数字时钟 DigitalClock

模拟时钟 AnalogClock

日期选择器 DatePicker(getYear,getMonth,get..)

时间选择器 TimePicker(getCurrentHour,getCurrentM...)

自己挨个加到main.xml文件里看看,感觉这玩意自己理解反而比别人教你更快,好像别人告你桌子上放的是橘子是酸的,不如你自己吃一个印象深刻,下面是我学习时写的,和我一样的新人同学可以复制走看看,但最好自己写,里面有xml文件,也有java文件,这个自己分的清楚吧

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

    <RadioGroup

    android:layout_width="fill_parent"

    android:layout_height="wrap_content" 

    android:orientation="horizontal">

<RadioButton 

android:id="@+id/radio1"

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text=""

    android:checked="true"

    />

    <RadioButton 

    android:id="@+id/radio2"

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text=""

    />

    </RadioGroup>

    <CheckBox 

     android:id="@+id/check1"

     android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="体育"/>

 <CheckBox 

     android:id="@+id/check2"

     android:layout_width="wrap_content" 

   android:layout_height="wrap_content" 

   android:text="音乐"/>

   <Button 

   android:id="@+id/button"

     android:layout_width="wrap_content" 

   android:layout_height="wrap_content" 

   android:text="确定"/>

   <DigitalClock 

   android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

   />

   <AnalogClock 

   android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

   />

    <DatePicker 

      android:id="@+id/date"

   android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

   />

   <TimePicker 

      android:id="@+id/time"

     android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

   />

</LinearLayout>

----------------------------------------------------------------

package com.li.BuJu;

 

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.RadioButton;

 

public class BuJu_yuanSuActivity extends Activity {

    /** Called when the activity is first created. */

RadioButton radio1,radio2;

CheckBox check1,check2;

Button button;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        radio1 = (RadioButton)findViewById(R.id.radio1);

        radio2 = (RadioButton)findViewById(R.id.radio2);

        check1 = (CheckBox)findViewById(R.id.check1);

        check2 = (CheckBox)findViewById(R.id.check2);

        button = (Button)findViewById(R.id.button);

        button.setOnClickListener(new OnClickListener() {

 

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

if(radio1.isChecked()){

System.out.println(radio1.getText());

}else{

System.out.println(radio2.getText());

}

if(check1.isChecked()){

System.out.println(check1.getText());

}if(check1.isChecked()){

System.out.println(check2.getText());

}

System.out.println(date.getYear()+","+date.getMonth()+","+date.getDayOfMonth());

System.out.println(time.getCurrentHour()+","+time.getCurrentMinute());

//月份从0开始,需要加1

}

});

    }

}

---------------------------------------------------------------------------------

ImageView处理图片的,,src属性指定图片位置(文件名全小写,没中文和空格)

android:src="@drawable/icon"

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

 

    >

<TextView  

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="请输入注册信息"

    />

<TableLayout   

android:layout_width="fill_parent" 

android:layout_height="wrap_content" 

android:stretchColumns="1">

<TableRow >

<TextView 

 android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="密码:"/>

    <EditText 

     android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    />

</TableRow>

<TableRow >

<TextView 

 android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="年龄:"/>

    <EditText 

     android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    />

</TableRow>

<TableRow >

<TextView 

 android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="用户名:"/>

    <EditText 

     android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    />

</TableRow>

<TableRow >

<TextView 

 android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="头像:"/>

    <LinearLayout >

    <ImageView 

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:src="@drawable/icon"

    />

    </LinearLayout>

</TableRow>

<TableRow >

<TextView 

 android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="性别:"/>

<RadioGroup 

android:layout_width="wrap_content" 

    android:layout_height="wrap_content"

android:orientation="horizontal">

<RadioButton 

android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text=""

    android:checked="true"/>

<RadioButton 

android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text=""/>

    </RadioGroup>

     </TableRow>

    <TableRow >

    <TextView 

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="爱好:"

    android:id="@+id/v_aihao"/>

<TableLayout 

android:layout_width="wrap_content" 

    android:layout_height="wrap_content" >

<TableRow >

    <CheckBox 

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="电影:"

    />

        <CheckBox 

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="体育:"

    />

    </TableRow>

    <TableRow >

        <CheckBox 

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="音乐:"

   />

        <CheckBox 

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="美术:"

  />

     </TableRow>

</TableLayout>

  

 

</TableRow>

<TableRow >

<TextView 

 android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="城市:"/>

    <Spinner 

     android:layout_width="wrap_content" 

    android:layout_height="wrap_content" />

</TableRow>

<LinearLayout 

   android:orientation="horizontal"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content">

<Button 

  android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_weight="1"

    android:text="确定"

    android:layout_margin="5dp"/>

<Button 

  android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_weight="1"

    android:text="取消"

     android:layout_margin="5dp"

    />

</LinearLayout>

</TableLayout>

</LinearLayout>

--------------------------------------------------------------------------

//android:paddingLeft="200dp"内边距

下拉菜单稍微难一点,一会再学

 

 

 

注意layout里的xml文件的文件名不能是大写英文字母,还有xml文件写错了不会像java文件那样的错误提示,一定要仔细.错误的话看logcat,没找到logcat的同学,点eclipse的window选项--Show View--LogCat或者Other找Android里面LogCat

还有xml文件里不能用//或/**/注释了,只能 <!-- 这里写注释--> 注释,且不能写在标签里

原创粉丝点击