[学习笔记]Android常用控件

来源:互联网 发布:c语言经典书 编辑:程序博客网 时间:2024/06/06 03:01

以下内容纯粹为本人学习笔记【记录】之用,所听课程(Q群群友百度网盘提供)为极客学院一位老师所讲(老师大名我尚未知晓),如有侵权请告知。在此特别感谢这位老师录制的视频资料。

widget n.窗口小部件、小器具、装饰品、[计算机]控件
component n.成分、组件、要素;a.成分的、组成的、构成的
1、下拉列表

2、日期选择器
1)New-EmptyActivity-命名为ChooseDate.java

package com.keen.learncomponents;import android.app.DatePickerDialog;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.DatePicker;public class ChooseDate extends AppCompatActivity {    private Button buttonChooseData;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_choose_date);        buttonChooseData = (Button) findViewById(R.id.buttonChooseData);        buttonChooseData.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                new DatePickerDialog(ChooseDate.this, new DatePickerDialog.OnDateSetListener() {                    @Override                    public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {                        System.out.println(String.format("%d-%d-%d", year, monthOfYear, dayOfMonth));                    }                } , 2017, 5, 23).show();            }        });    }}

2)布局文件xml添加一个按钮,用于打开日期选择器

    <Button        android:text="0000-00-00"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/buttonChooseData" />

3)AndroidManifest.xml注释掉MainActivity启动,让ChooseDate Activity作为Launcher。

        <activity android:name=".MainActivity">            <!--<intent-filter>-->                <!--<action android:name="android.intent.action.MAIN" />-->                <!--<category android:name="android.intent.category.LAUNCHER" />-->            <!--</intent-filter>-->        </activity>

这里写图片描述

如何将这个日期呈现在按钮里头?修改onDateSet()方法里:

String theDate = String.format("%d-%d-%d", year, monthOfYear, dayOfMonth);                        System.out.println(theDate);                        buttonChooseData.setText(theDate);

这里写图片描述
3、时间选择器
1)New-EmptyActivity-并作为Launcher Activity:ChooseTime

package com.keen.learncomponents;import android.app.TimePickerDialog;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TimePicker;public class ChooseTime extends AppCompatActivity {    private Button buttonChooseTime;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_choose_time);        buttonChooseTime = (Button) findViewById(R.id.buttonChooseTime);        buttonChooseTime.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                new TimePickerDialog(ChooseTime.this, new TimePickerDialog.OnTimeSetListener() {                    @Override                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {                        String theTime = String.format("%d:%d", hourOfDay, minute);                        System.out.println(theTime);                        buttonChooseTime.setText(theTime);                    }                },0,0,true).show();            }        });    }}

2)同时,往对应的布局文件xml添加一个按钮

    <Button        android:text="00:00"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/buttonChooseTime" />

3)Androidmanifest.xml将其他Activity Launcher注释掉
这里写图片描述

4、单项选择
1)New-EmptyActivity-并作为Launcher Activity:SingleChoose

package com.keen.learncomponents;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.RadioButton;import android.widget.Toast;public class SingleChoose extends AppCompatActivity {    private Button btnSubmit;    private RadioButton rbD;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_single_choose);        btnSubmit = (Button) findViewById(R.id.btnSubmit);        rbD = (RadioButton) findViewById(R.id.rbD);        btnSubmit.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                if (rbD.isChecked()) {                    Toast.makeText(SingleChoose.this, "恭喜您!看来你很了解啊!", Toast.LENGTH_SHORT).show();                }else {                    Toast.makeText(SingleChoose.this, "你选的选项是你良心选上的吗?赶紧再选一个吧,哈哈", Toast.LENGTH_SHORT).show();                }            }        });    }}

2)同时,修改对应的布局文件xml
radio n./v (发)无线电、收音机
radiogroup n.单项选择

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_single_choose"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.keen.learncomponents.SingleChoose">    <TextView        android:text="植物大战僵尸里的一大波僵尸是什么意思?"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/textView" />    <RadioGroup        android:layout_width="wrap_content"        android:layout_height="wrap_content" >        <RadioButton            android:text="A 没玩过介个"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/rbA"            android:layout_weight="1" />        <RadioButton            android:text="B 烫大波浪的僵尸"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/rbB"            android:layout_weight="1" />        <RadioButton            android:text="C 一大群僵尸"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/rbC"            android:layout_weight="1" />        <RadioButton            android:text="D 一群波很大的僵尸"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/rbD"            android:layout_weight="1" />    </RadioGroup>    <Button        android:text="提交"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/btnSubmit" /></LinearLayout>

3)Androidmanifest.xml将其他Activity Launcher注释掉
这里写图片描述
5、多项选择
checkbox
1)New-EmptyActivity-并作为Launcher Activity:MulChoose

package com.keen.learncomponents;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.TextView;public class MulChoose extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {    private CheckBox cb1,cb2,cb3,cb4;    private TextView tvResults;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_mul_choose);        cb1 = (CheckBox) findViewById(R.id.cb1);        cb2 = (CheckBox) findViewById(R.id.cb2);        cb3 = (CheckBox) findViewById(R.id.cb3);        cb4 = (CheckBox) findViewById(R.id.cb4);        tvResults = (TextView) findViewById(R.id.tvResults);        //添加事件监听器        cb1.setOnCheckedChangeListener(this);        cb2.setOnCheckedChangeListener(this);        cb3.setOnCheckedChangeListener(this);        cb4.setOnCheckedChangeListener(this);    }    @Override    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {        String str = "你怎么选的是";        if (cb1.isChecked()) {            str+= cb1.getText() + ",";        }        if (cb2.isChecked()) {            str+= cb2.getText() + ",";        }        if (cb3.isChecked()) {            str+= cb3.getText() + ",";        }        if (cb4.isChecked()) {            str+= cb4.getText();        }        tvResults.setText(str);    }}

2)同时,修改对应的布局文件xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_mul_choose"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.keen.learncomponents.MulChoose">    <TextView        android:text="一个喝醉了的老鼠会干什么?"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/tvSubject" />    <CheckBox        android:text="回去睡觉"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/cb1" />    <CheckBox        android:text="找猫去"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/cb2" />    <CheckBox        android:text="喝杯解酒茶"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/cb3" />    <CheckBox        android:text="继续把酒瓶也吃了"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/cb4" />    <TextView        android:text="TextView"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/tvResults" /></LinearLayout>

3)Androidmanifest.xml将其他Activity Launcher注释掉
这里写图片描述

原创粉丝点击