27.02.2014 Views

Android 开发教程

Android 开发教程

Android 开发教程

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

-----------------------------------<strong>Android</strong> 编 程 基 础<br />

3 修 改 mainActivity.java, 添 加 自 动 完 成 功 能<br />

package zyf.Ex_Ctrl_13ME;<br />

/* 导 入 使 用 的 包 */<br />

import android.app.Activity;<br />

import android.os.Bundle;<br />

import android.view.View;<br />

import android.widget.ArrayAdapter;<br />

import android.widget.AutoCompleteTextView;<br />

import android.widget.Button;<br />

import android.widget.TextView;<br />

public class Ex_Ctrl_13ME extends Activity {<br />

/** Called when the activity is first created. */<br />

/* 定 义 要 使 用 的 类 对 象 */<br />

private String[] normalString =<br />

new String[] {<br />

"<strong>Android</strong>", "<strong>Android</strong> Blog","<strong>Android</strong> Market", "<strong>Android</strong> SDK",<br />

"<strong>Android</strong> AVD","BlackBerry","BlackBerry JDE", "Symbian",<br />

"Symbian Carbide", "Java 2ME","Java FX", "Java 2EE",<br />

"Java 2SE", "Mobile", "Motorola", "Nokia", "Sun",<br />

"Nokia Symbian", "Nokia forum", "WindowsMobile", "Broncho",<br />

"Windows XP", "Google", "Google <strong>Android</strong> ", "Google 浏 览 器 ",<br />

"IBM", "MicroSoft", "Java", "C++", "C", "C#", "J#", "VB" };<br />

@SuppressWarnings("unused")<br />

private TextView show;<br />

private AutoCompleteTextView autoTextView;<br />

private Button clean;<br />

private ArrayAdapter arrayAdapter;<br />

@Override<br />

public void onCreate(Bundle savedInstanceState) {<br />

super.onCreate(savedInstanceState);<br />

/* 装 入 主 屏 布 局 main.xml*/<br />

setContentView(R.layout.main);<br />

/* 从 XML 中 获 取 UI 元 素 对 象 */<br />

show = (TextView) findViewById(R.id.TextView_InputShow);<br />

autoTextView =<br />

(AutoCompleteTextView) findViewById(R.id.AutoCompleteTextView_input);<br />

clean = (Button) findViewById(R.id.Button_clean);<br />

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

arrayAdapter = new ArrayAdapter(this<br />

this,<br />

android.R.layout.simple_dropdown_item_1line, normalString);<br />

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

autoTextView.setAdapter(arrayAdapter);<br />

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

30

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!