• 沒有找到結果。

MultiAutoCompleteTextView 与数组

在文檔中 开放手机联盟 (頁 173-181)

① 新建工程

② 修改 main.xml 布局,添加一个 TextView、一个 MultiAutoCompleteTextView、一个 Button

③ MultiAutoCompleteTextView 是可以自动完成用户输入,能够添加多个输入,注意设置 Tokenizer

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

<AbsoluteLayout

android:id="@+id/widget0"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

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

<TextView

android:id="@+id/TextView_InputShow"

android:layout_width="228px"

android:layout_height="47px"

android:text="请输入"

android:textSize="25px"

android:layout_x="42px"

android:layout_y="37px"/>

<MultiAutoCompleteTextView

android:layout_width="275px"

android:layout_height="wrap_content"

android:text=""

android:id="@+id/MultiAutoCompleteTextView"

android:layout_x="23px"

android:layout_y="98px"/>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_x="127dip"

android:text="清空"

android:id="@+id/Button_clean"

android:layout_y="180dip"/>

</AbsoluteLayout>

/*设置Tokenizer来确定用户输入文本的相关范围*/

myAutoCompleteTextView

.setTokenizer(newnewnewnew MultiAutoCompleteTextView.CommaTokenizer());

33

④ 修改 mainActivity.java,实现动态自动完成用户输入相关内容 package

package package

package zyf.Ex_Ctrl_13_B;

/*导入使用的包*/

import import import

import android.app.Activity;

import import import

import android.os.Bundle;

import import import

import android.view.View;

import import import

import android.widget.ArrayAdapter;

import import import

import android.widget.Button;

import import import

import android.widget.MultiAutoCompleteTextView;

public public public

public classclassclassclass Ex_Ctrl_13_B extendsextendsextendsextends Activity {

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

/*定义要使用的类对象*/

private private private

private String[] normalString = new

new new

new String[] {

"Android", "Android Blog","Android Market", "Android SDK",

"Android AVD","BlackBerry","BlackBerry JDE", "Symbian",

"Symbian Carbide", "Java 2ME","Java FX", "Java 2EE",

"Java 2SE", "Mobile", "Motorola", "Nokia", "Sun",

"Nokia Symbian", "Nokia forum", "WindowsMobile", "Broncho",

"Windows XP", "Google", "Google Android ", "Google 浏览器",

"IBM", "MicroSoft", "Java", "C++", "C", "C#", "J#", "VB" };

private private private

private Button clean;

private private private

private MultiAutoCompleteTextView myAutoCompleteTextView;

private private private

private ArrayAdapter<String> adapter;

@Override public public public

public voidvoidvoidvoid onCreate(Bundle savedInstanceState) { super

super super

super.onCreate(savedInstanceState);

/* 装入主屏布局main.xml */

setContentView(R.layout.main);

/* 以findViewById()从XML中获取UI元素对象 */

myAutoCompleteTextView =

(MultiAutoCompleteTextView) findViewById(R.id.MultiAutoCompleteTextView);

clean = (Button) findViewById(R.id.Button_clean);

/* new ArrayAdapter 对象并将normalString 字符串数组传入 */

/* 实现一个适配器对象,用来给自动完成输入框添加自动装入的内容 */

adapter = newnewnewnew ArrayAdapter<String>(thisthisthisthis,

android.R.layout.simple_dropdown_item_1line, normalString);

/* 将ArrayAdapter 添加AutoCompleteTextView 对象中 */

/* 给自动完成输入框添加内容适配器 */

myAutoCompleteTextView.setAdapter(adapter);

/*设置Tokenizer来确定用户输入文本的相关范围*/

myAutoCompleteTextView

34

⑤ 结果

/* 给清空按钮添加点击事件处理监听器 */

clean.setOnClickListener(newnewnewnew Button.OnClickListener() {

@Override public public public

public voidvoidvoidvoid onClick(View v) {

// TODOTODOTODOTODO Auto-generated method stub /* 清空 */

myAutoCompleteTextView.setText("");

} });

} }

35

模拟////数字////线程小时钟设计

AnalogClock 与 DigitalClock 的原理--线程时钟实现

① 新建工程

② 修改 man.xml 布局,添加一个 AnalogClock、一个 DigitalClock、一个 TextView

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

<AbsoluteLayout

android:id="@+id/widget0"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

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

<TextView

android:id="@+id/TextView_showTime"

android:layout_width="200px"

android:layout_height="39px"

android:textSize="25px"

android:layout_x="82px"

android:layout_y="222px">

</TextView>

<AnalogClock

android:id="@+id/Clock"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_x="82px"

android:layout_y="34px">

</AnalogClock>

<DigitalClock

android:id="@+id/DigitalClock01"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="上午1:01"

android:textSize="25px"

android:layout_x="82px"

android:layout_y="300px">

</DigitalClock>

<TextView

android:id="@+id/widget46"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="模拟时钟"

android:layout_x="11px"

36

③ 模拟时钟的实现不需要额外代码,只需要 UI 中添加,其自动显示时间

④ 数字时钟的实现也不需要额外代码,只需要 UI 中添加,其自动显示时间

⑤ 而使用线程实现的 TextView 时钟则需要线程 Thread、Handler(发送、处理消息)辅助实现

</TextView>

<TextView

android:id="@+id/widget47"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="线程时钟"

android:layout_x="11px"

android:layout_y="228px">

</TextView>

<TextView

android:id="@+id/widget48"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="数字时钟"

android:layout_x="11px"

android:layout_y="308px">

</TextView>

</AbsoluteLayout>

/*定义模拟时钟对象*/

AnalogClock myClock;

/*从XML中获取模拟时钟UI对象*/

myClock=(AnalogClock)findViewById(R.id.Clock);

/*定义数字时钟对象*/

DigitalClock myDigClock;

/*从XML中获取数字时钟UI对象*/

myDigClock=(DigitalClock)findViewById(R.id.DigitalClock01);

import import import

import android.os.Handler;

import import import

import android.os.Message;

public public public

public classclassclassclass Clock extendsextendsextendsextends Activity implementsimplementsimplementsimplements Runnable{

public public public

public Handler myHandler;

@Override protected protected protected

protected voidvoidvoidvoid onCreate(Bundle savedInstanceState) { // TODOTODOTODOTODO Auto-generated method stub

super super super

super.onCreate(savedInstanceState);

setContentView(R.layout.gh);

myHandler = newnewnewnew Handler() {

@Override

37

⑥ 修改 mianActivity.java,实现线程控制,以及模拟/数字时钟的实现 public

public public

public voidvoidvoidvoid handleMessage(Message msg) { // TODOTODOTODOTODO Auto-generated method stub }

};

Thread myT = newnewnewnew Thread(thisthisthisthis);

myT.start();

public voidvoidvoidvoid run() {

// TODOTODOTODOTODO Auto-generated method stub }

}

package package package

package zyf.three.clock;

/*导入要使用的包*/

import import import

import java.util.Calendar;

import import import

import android.app.Activity;

import import import

import android.os.Bundle;

import import import

import android.os.Handler;

import import import

import android.os.Message;

import import import

import android.widget.AnalogClock;

import import import

import android.widget.DigitalClock;

import import import

import android.widget.TextView;

public public public

public classclassclassclass Clock extendsextendsextendsextends Activity implementsimplementsimplementsimplements Runnable { /* 定义要使用的类对象 */

private private private

private TextView showTime;// 显示进程时钟的TextView AnalogClock myClock;// 模拟时钟

DigitalClock myDigClock;// 数字时钟 private

private private

private finalfinalfinalfinal intintintint msg_Key = 0x1234;// 发送的消息内容 public

public public

public Handler myHandler;// 发送、处理消息的类 public

public public

public Calendar myCalendar;// 日历类 private

private private

private intintintint my_Hour, my_Minute, my_Second;// 时、分、秒 private

private private

private Thread myT;

@Override protected protected protected

protected voidvoidvoidvoid onCreate(Bundle savedInstanceState) { // TODOTODOTODOTODO Auto-generated method stub

super super super

super.onCreate(savedInstanceState);

/* 导入主屏布局main.xml */

setContentView(R.layout.gh);

/* 从XML中获取模拟时钟UI对象 */

38

/* 从XML中获取数字时钟UI对象 */

myDigClock = (DigitalClock) findViewById(R.id.DigitalClock01);

/* 从XML中获取TextView UI对象 */

showTime = (TextView) findViewById(R.id.TextView_showTime);

/* 通过Handler 来接收进程所传递的信息并更新TextView */

myHandler = newnewnewnew Handler() {

@Override public public public

public voidvoidvoidvoid handleMessage(Message msg) { /* 这里是处理信息的方法 */

// TODOTODOTODOTODO Auto-generated method stub super

switch (msg.what) { case

case case

case msg_Key:

/* 在这处理要TextView对象Show时间的事件 */

showTime.setText(my_Hour + " : " + my_Minute + " : "

+ my_Second);

break

myT = newnewnewnew Thread(thisthisthisthis);

myT.start();

public voidvoidvoidvoid run() {

// TODOTODOTODOTODO Auto-generated method stub try

long Time = System.currentTimeMillis();

myCalendar = Calendar.getInstance();

myCalendar.setTimeInMillis(Time);

my_Hour = myCalendar.get(Calendar.HOUR);

my_Minute = myCalendar.get(Calendar.MINUTE);

my_Second = myCalendar.get(Calendar.SECOND);

/* 让进程休息一秒 */

Thread.sleep(1000);

39

⑦ 结果

/* 重要关键程序:取得时间后发出信息给Handler */

Message msg = newnewnewnew Message();

msg.what = msg_Key;

myHandler.sendMessage(msg);

/* 重要关键程序:取得时间后发出信息给Handler */

} whilewhilewhilewhile (myT.interrupted() == falsefalsefalsefalse);

/* 当系统发出中断信息时停止本循环 */

} catchcatchcatchcatch (InterruptedException e) { // TODOTODOTODOTODO Auto-generated catch block e.printStackTrace();

} } }

40

在文檔中 开放手机联盟 (頁 173-181)