Android 基本控件

layout頁面

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



<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none"
    android:layout_margin="10dp"
    >
    /**
    *ScrollView 滾動視圖
    */

    <LinearLayout
        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:layout_marginTop="30dp"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:marqueeRepeatLimit="marquee_forever"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:text="跑馬燈效果2016abcdefghijklmnopqrstuvwxyz123456789"
            android:textSize="20sp"
            android:textColor="#ff00ff"/>
        /**
        *複選框
        */
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="請選擇就業方向"
            android:padding="10dp"/>
        <CheckBox
            android:id="@+id/cb_android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="android"/>
        <CheckBox
            android:id="@+id/cb_ios"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ios"/>
        <CheckBox
            android:id="@+id/cb_wp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="wp"/>
        <Button
            android:id="@+id/bt_show"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="顯示結果"
            android:padding="10dp"/>

        /**
        *單選框
        */

        <RadioGroup
            android:id="@+id/rg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <RadioButton
                android:id="@+id/rb_man"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="男" />

            <RadioButton
                android:id="@+id/rb_woman"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="女" />
        </RadioGroup>

        <ImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:scaleType="fitEnd"
            android:src="@drawable/abc" />

        /**
        *日期選擇器
        */

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <EditText
                android:id="@+id/et_date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="請輸入日期"
                android:layout_toLeftOf="@+id/bt_choose"/>
            <Button
                android:id="@+id/bt_choose"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="選擇日期"
                android:layout_alignParentRight="true"/>
        </RelativeLayout>

        /**
        *時間選擇器
        */

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <EditText
                android:id="@+id/et_time"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="請輸入時間"
                android:layout_toLeftOf="@+id/bt_choose_time"/>
            <Button
                android:id="@+id/bt_choose_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="選擇時間"
                android:layout_alignParentRight="true"/>

        </RelativeLayout>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="請選擇開始,啓動進度條"
            android:padding="20dp"/>

        /**
        *進度條
        */

        <ProgressBar
            android:id="@+id/pb"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
            android:max="100"/>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <Button
                android:id="@+id/bt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="開始"/>
            <Button
                android:id="@+id/bt1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="還原"
                android:layout_alignParentRight="true"/>
        </RelativeLayout>



        <ImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:scaleType="fitEnd"
            android:src="@drawable/abc" />




    </LinearLayout>
</ScrollView>

java頁面

package com.zdsoft.my112401;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.graphics.RadialGradient;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private CheckBox cb_android,cb_ios,cb_wp;
    private Button bt_show;
    private List<CheckBox> list = new ArrayList<CheckBox>();

    private RadioGroup rg;
    private RadioButton rb_man, rb_woman;

    private Button bt_choose,bt_choose_time;
    private EditText et_date,et_time;

    private Button bt,bt1;
    private ProgressBar pb;
    private int count=0;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
//         複選框
        bt_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuffer sb= new StringBuffer();
                for (CheckBox cb:list){
                    if(cb.isChecked()){
                        sb.append(cb.getText().toString()+"\t");
                    }
                }
                Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_SHORT).show();
            }
        });

//         單選框
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (rb_man.isChecked()) {
                    Toast.makeText(MainActivity.this, rb_man.getText().toString(), Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, rb_woman.getText().toString(), Toast.LENGTH_SHORT).show();
                }
            }
        });
//        日期、時間選擇器調用
        bt_choose.setOnClickListener(this);
        bt_choose_time.setOnClickListener(this);

//         進度條
        bt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                final  Handler handler=new Handler(){
                    @Override
                    public void handleMessage(Message msg) {
                        if(msg.what==1){
                           pb.setProgress(count);
                        }
                    }
                };

                new Thread(){
                    @Override
                    public void run() {
                        while (count<100){
                            count++;
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }handler.sendEmptyMessage(1);

                        }
                    }
                }.start();

            }
        });
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count=0;
                pb.setProgress(count);

            }
        });

    }

    /**
     * 控件初始化
     */

    public void initView() {

        rg = (RadioGroup) findViewById(R.id.rg);
        rb_man = (RadioButton) findViewById(R.id.rb_man);
        rb_woman = (RadioButton) findViewById(R.id.rb_woman);
//        設置默認時選男
        rb_man.setChecked(true);

        bt_choose= (Button) findViewById(R.id.bt_choose);
        et_date= (EditText) findViewById(R.id.et_date);

        bt_choose_time= (Button) findViewById(R.id.bt_choose_time);
        et_time= (EditText) findViewById(R.id.et_time);

        bt= (Button) findViewById(R.id.bt);
        bt1= (Button) findViewById(R.id.bt1);
        pb= (ProgressBar) findViewById(R.id.pb);

        cb_android= (CheckBox) findViewById(R.id.cb_android);
        cb_ios= (CheckBox) findViewById(R.id.cb_ios);
        cb_wp= (CheckBox) findViewById(R.id.cb_wp);
        bt_show= (Button) findViewById(R.id.bt_show);
        list.add(cb_android);
        list.add(cb_ios);
        list.add(cb_wp);

    }

    /**
     *
     * 實現View.OnClickListener時重寫onClick方法
     */

    @Override
    public void onClick(View v) {
        Calendar calendar=Calendar.getInstance();
        final int year = calendar.get(Calendar.YEAR);
        final int month = calendar.get(Calendar.MONTH);
        final int day = calendar.get(Calendar.DAY_OF_MONTH);
        final int hour=calendar.get(Calendar.HOUR_OF_DAY);
        final int minute=calendar.get(Calendar.MINUTE);
        switch (v.getId()){
            case R.id.bt_choose:

                DatePickerDialog dpd=new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        et_date.setText(year+"-"+(month+1)+"-"+day);
                    }
                }, year, month, day);
                dpd.show();
                break;
            case R.id.bt_choose_time:


                TimePickerDialog tpd=new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        et_time.setText(hourOfDay+":"+minute);
                    }
                }, hour, minute, false);
                tpd.show();
                break;
            default:break;

        }

    }
}



發佈了6 篇原創文章 · 獲贊 5 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章