系出名门Android(6) - 控件(View)之DatePicker, TimePicker, ToggleButton, EditText, Progre

来源:互联网 发布:全景图制作java 编辑:程序博客网 时间:2021/01/28 17:27
系出名门Android(6) - 控件(View)之DatePicker, TimePicker, ToggleButton, EditText, ProgressBar, SeekBar, AutoCompleteTextView, MultiAutoCompleteTextView


作者:webabcd


介绍
在 Android 中使用各种控件(View)
DatePicker - 日期选择控件
TimePicker - 时间选择控件
ToggleButton - 双状态按钮控件
EditText - 可编辑文本控件
ProgressBar - 进度条控件
SeekBar - 可拖动的进度条控件
AutoCompleteTextView - 支持自动完成功能的可编辑文本控件
MultiAutoCompleteTextView - 支持自动完成功能的可编辑文本控件,允许输入多值(多值之间会自动地用指定的分隔符分开)


1、DatePicker 的 Demo
datepicker.xml

代码
<?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">
   
    <!--
        DatePicker - 日期选择控件
    -->
    <DatePicker android:id="@+id/datePicker"
        android:layout_width="wrap_content" android:layout_height="wrap_content">
    </DatePicker>
   
</LinearLayout>


_DatePicker.java

代码
package com.webabcd.view;

import android.app.Activity;
import android.os.Bundle;

public class _DatePicker extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.datepicker);

        // 具体的应用可参见对话框中的示例
        setTitle("DatePicker");
    }
}



2、TimePicker 的 Demo
timepicker.xml

代码
<?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">
   
    <!--
        TimePicker - 时间选择控件
    -->
    <TimePicker android:id="@+id/timePicker"
        android:layout_width="wrap_content" android:layout_height="wrap_content">
    </TimePicker>
   
</LinearLayout>


_TimePicker.java

代码
package com.webabcd.view;

import android.app.Activity;
import android.os.Bundle;

public class _TimePicker extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.timepicker);

        // 具体的应用可参见对话框中的示例
        setTitle("TimePicker");
    }
}



3、ToggleButton 的 Demo
togglebutton.xml

代码
<?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:id="@+id/textView" />
       
    <!--
        ToggleButton - 双状态按钮控件
            textOn - 当按钮状态为 true 时所显示的文本
            textOff - 当按钮状态为 false 时所显示的文本
    -->
    <ToggleButton android:id="@+id/toggleButton"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:textOn="关闭" android:textOff="打开" />
       
</LinearLayout>


_ToggleButton.java

代码
package com.webabcd.view;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ToggleButton;

public class _ToggleButton extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.togglebutton);

        setTitle("ToggleButton");
       
        final ToggleButton btn = (ToggleButton) this.findViewById(R.id.toggleButton);
        // setOnClickListener() - 响应按钮的鼠标单击事件
        btn.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                TextView txt = (TextView) _ToggleButton.this.findViewById(R.id.textView);
                // ToggleButton.isChecked() - 双状态按钮的按钮状态
                txt.setText("按钮状态:" + String.valueOf(btn.isChecked()));
            }
        });
    }
}



4、EditText 的 Demo
edittext.xml

代码
<?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">
   
    <!--
        EditText - 可编辑文本控件
    -->
    <EditText android:id="@+id/editText" android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </EditText>
   
</LinearLayout>


_EditText.java

代码
package com.webabcd.view;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;

public class _EditText extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.edittext);

        setTitle("EditText");
       
        EditText txt = (EditText) this.findViewById(R.id.editText);
        txt.setText("我可编辑");
    }
}



5、ProgressBar 的 Demo
progressbar.xml

代码
<?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">

    <!--
        ProgressBar - 进度条控件
    -->

    <!--以下分别为大、中、小的进度条控件(圆圈状)-->
    <ProgressBar android:id="@+android:id/progress_large"
        style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ProgressBar android:id="@+android:id/progress"
        android:layout_width="wrap_content" android:layout_height="wrap_content" />
    <ProgressBar android:id="@+android:id/progress_small"
        style="?android:attr/progressBarStyleSmall" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <!--
        进度条控件(条状)的演示
            style - 进度条的样式,本例使用内置样式
            max - 进度的最大值
            progress - 第一进度位置
            secondaryProgress - 第二进度位置
    -->
    <ProgressBar android:id="@+id/progress_horizontal"
        style="?android:attr/progressBarStyleHorizontal" android:layout_width="200px"
        android:layout_height="wrap_content" android:max="100"
        android:progress="50" android:secondaryProgress="75" />

</LinearLayout>


_ProgressBar.java

代码
package com.webabcd.view;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;


// 另见对话框中的进度条
public class _ProgressBar extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);       
       
        // 设置特性以允许在应用程序的标题栏上显示进度条(条状)
        requestWindowFeature(Window.FEATURE_PROGRESS);
        // 设置特性以允许在应用程序的标题栏上显示进度条(圆圈状)
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

        this.setContentView(R.layout.progressbar);

        setTitle("ProgressBar");
       
        // 在标题栏上显示进度条(条状)
        setProgressBarVisibility(true);
        // 在标题栏上显示进度条(圆圈状)
        setProgressBarIndeterminateVisibility(true);
       
        // 指定进度条的进度
        setProgress(50 * 100);
        setSecondaryProgress(75 * 100);
    }
}



6、SeekBar 的 Demo
seekbar.xml

代码
<?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">
   
    <!--
        SeekBar - 可拖动的进度条控件
            max - 进度的最大值
            progress - 第一进度位置
            secondaryProgress - 第二进度位置
    -->
    <SeekBar android:id="@+id/seekBar" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:max="100"
        android:progress="50" android:secondaryProgress="75" />

    <TextView android:id="@+id/progress" android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView android:id="@+id/tracking" android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


_SeekBar.java

代码
package com.webabcd.view;

import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;

public class _SeekBar extends Activity implements
        SeekBar.OnSeekBarChangeListener {

    SeekBar mSeekBar;
    TextView mProgressText;
    TextView mTrackingText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.seekbar);

        setTitle("SeekBar");

        mSeekBar = (SeekBar) findViewById(R.id.seekBar);
        // setOnSeekBarChangeListener() - 响应拖动进度条事件
        mSeekBar.setOnSeekBarChangeListener(this);
       
        mProgressText = (TextView) findViewById(R.id.progress);
        mTrackingText = (TextView) findViewById(R.id.tracking);
    }

    // 拖动进度条后,进度发生改变时的回调事件
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromTouch) {
        mProgressText.setText(progress + "%");
    }

    // 拖动进度条前开始跟踪触摸
    public void onStartTrackingTouch(SeekBar seekBar) {
        mTrackingText.setText("开始跟踪触摸");
    }

    // 拖动进度条后停止跟踪触摸
    public void onStopTrackingTouch(SeekBar seekBar) {
        mTrackingText.setText("停止跟踪触摸");
    }

}



7、AutoCompleteTextView 的 Demo
autocompletetextview.xml

代码
<?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">

    <!--
        AutoCompleteTextView - 支持自动完成功能的可编辑文本控件
    -->
    <AutoCompleteTextView android:id="@+id/editText"
        android:layout_width="fill_parent" android:layout_height="wrap_content" />

</LinearLayout>


_AutoCompleteTextView.java

代码
package com.webabcd.view;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class _AutoCompleteTextView extends Activity {

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

        setTitle("AutoCompleteTextView");
       
        // 实例化适配器,指定显示格式及数据源
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this,
                android.R.layout.simple_dropdown_item_1line,
                ary);
        AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.editText);
        // 指定自动完成控件的适配器
        textView.setAdapter(adapter);
    }

    // 自动完成控件的所需数据的数据源
    private String[] ary = new String[] {
        "abc",
        "abcd",
        "abcde",
        "abcdef",
        "abcdefg",
        "hij",
        "hijk",
        "hijkl",
        "hijklm",
        "hijklmn",
    };
}



8、MultiAutoCompleteTextView 的 Demo
multiautocompletetextview.xml

代码
<?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">

    <!--
        MultiAutoCompleteTextView - 支持自动完成功能的可编辑文本控件,允许输入多值(多值之间会自动地用指定的分隔符分开)
    -->
    <MultiAutoCompleteTextView android:id="@+id/editText"
        android:layout_width="fill_parent" android:layout_height="wrap_content" />

</LinearLayout>


_MultiAutoCompleteTextView.java

代码
package com.webabcd.view;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.MultiAutoCompleteTextView;

public class _MultiAutoCompleteTextView extends Activity {

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

        setTitle("MultiAutoCompleteTextView");
       
        // 实例化适配器,指定显示格式及数据源
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this,
                android.R.layout.simple_dropdown_item_1line,
                ary);
        MultiAutoCompleteTextView textView = (MultiAutoCompleteTextView) findViewById(R.id.editText);
        textView.setAdapter(adapter);
       
        // 设置多个值之间的分隔符,此处为逗号
        textView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
    }

    // 自动完成控件的所需数据的数据源
    private String[] ary = new String[] {
        "abc",
        "abcd",
        "abcde",
        "abcdef",
        "abcdefg",
        "hij",
        "hijk",
        "hijkl",
        "hijklm",
        "hijklmn",
    };
}