为 AutoComplete 创建一个活动
为 AutoComplete AutoComplete AutoComplete AutoComplete 创建一个活动 第八章(4) (4) (4) (4)
在 本 节 中 , 你 将 创 建 一 个 突 出 AutoCompleteTextView 的 活 动 。
AutoCompleteTextView 对你的有应用程序来说可以成为一个非常有力的工具。
特别是对于 Android 主屏幕有限的空间来说。
AutoCompleteTextView,正如这个名字所说,是修改后的 TextView,而它可以
参考到可用的单词或者短语并自动完成输入。这样的 Views 是在移动应用程序里
是非常有用的当你不想花费大量的空间到一个 ListView,或者你想要加速你输
入文本的过程。
要开始为 AutoCompleteTextView 创建活动,你需要为布局增加一个新
的.xml 文件,为代码增加一个.java 文件,并且一个 Intent 过滤器来处理呼叫。
提示
创建这些条目的过程出现在本章“构造活动”一节中。创建下面项目的部分时可
以参考那个部分。
创建一个 autocomplete.xml 文件
在 你 的 AndroidViews 项 目 中 创 建 一 个 新 的 .xml 文 件 , 并 命 名 为
autocomplete.xml。记住文件名必须用小写。这个文件应当出现在 layout 文件
夹。双击这个文件来编辑它。这个文件会控制 AutoCompleteTextView 活动的布
局 , 所 以 你 需 要 在 布 局 中 有 一 个 AutoCompleteTextView 。 增 加 过
AutoCompleteTextView 的 XML 文件应当如下:
你已经在.xml 文件中创建了几个 Views 了,所以你应当熟悉这个格式。对于
AutoCompleteTextView,没什么特别之处。你设定 id 到 testAutoComplete,还
有相应的宽度和高度到 fill_parent 和 wrap_content,还应该为两个按钮增加
布局。这些按钮将被用于你将改变的属性控制。命名按钮为 autoCompleteButton
和 textColorButton,如下:
<AutoCompleteTextView
android:id="@+id/testAutoComplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button android:id="@+id/autoCompleteButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Change Layout"/>
<Button android:id="@+id/textColorButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"84
有了新增的三个 View 布局,你完成的 autocomplete.xml 文件看上去应该像
这样:
创建一个 autocomplete.java 文件
跟从本章“创建一个新的 java 文件”的介绍。第一件要做的事就是为你的
Views 输入包装。在这个活动中,使用了两个 Views,AutoCompleteTextView 和
按钮。你还需要设置颜色和一个 ArrayAdapter。因此,输入下面包装到活动中:
注意
现在你可能不知道它们的用途,先加入它们吧,我会解释的。
为 AutoCOmplete 类增加初始的结构到 autocomplete.java 文件中:
android:text="Change Text Color"/>
<?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 android:id="@+id/testAutoComplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button android:id="@+id/autoCompleteButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Change Layout"/>
<Button android:id="@+id/textColorButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Change Text Color"/>
</LinearLayout>
package android_programmers_guide.AndroidViews;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.graphics.Color;
public class AutoComplete extends Activity {
@Override85
这个类给了你建造活动其它部分的基础。这个活动的所有功能将会被围绕这个类
建造。第一件要做的事就是从 autocomplete.xml 中装载布局:
对于本例,将创建 AutoComplete TextView,所以它包含一年中的月份。当一个
用户在框中输入,它会猜测那个月份用户试图去输入。假定 AutoComplete
TextView 将 包 含 月 份 的 清 单 , 你 需 要 来 创 建 一 个 可 以 被 赋 值 到
AutoCompleteTextView 的清单。
创建字符串数组并赋值月份数值到其中:
下一个任务是复制这个字符串到 AutoCompleteTextView。到目前为止,你已经
创建了一些 Views 了。所以,创建 AutoCompleteTextView 的代码看上去应该很
熟悉。你之前没看到过的就是把字符串赋值给 View:
在 第 一 行 , 拿 去 创 建 的 字 符 串 数 组 并 且 复 制 到 一 个 名 为 monthArray 的
ArrayAdapter 。 下 一 步 , 你 通 过 在 .xml 文 件 中 定 位 来 例 示
AutoCompleteTextView 。 最 后 , 使 用 setAdapter() 方 法 来 赋 值 monthArray
ArrayAdapter 到 AutoCompleteTextView 中。
下一个零星的代码例示那两个按钮。与上一章的代码相同。唯一和你所写代码不
同的是你正在呼叫两个函数,changeOption 和 changeOption2,而这些,你还
没有创建呢。
注意
你在传递 AutoCompleteTextView 到函数呼叫。当你创建函数还需要创建参数。
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
}
}
setContentView(R.layout.autocomplete);
static final String[] Months = new String[]{
"January","February","March","April","May","June","July","August",
"September","October","November","December"
};
ArrayAdapter<String> monthArray = new
ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, Months);
final AutoCompleteTextView textView =
(AutoCompleteTextView)
findViewById(R.id.testAutoComplete);
textView.setAdapter(monthArray);
final Button changeButton = (Button)
findViewById(R.id.autoCompleteButton);
changeButton.setOnClickListener(new Button.OnClic86
被这些按钮呼叫的函数将被用于在 AutoComplete TextView 改变布局属性。这两
个我选择来修改的属性(通过两个按钮)是布局的高度和文本的颜色。你将设置
一个按钮来改变 AutoCompleteTextView 的布局高度,从 30 到 100 并且返回。另
一个按钮将改变 View 内文本的为红色。
函数 changeOption()会改变 AutoCompleteTextView 的布局高度。代码非常的简
单:
在这个函数中你要做的就是检查当前 AutoCompleteTextView 的高度。如果高度
是 100,把它设为 30,否则设为 100。
changeOption2()函数也简单:
这个函数简单的把 AutoCompleteTextView 的文本颜色设为 Color.RED。
Color.RED 的数值从 android.graphics.Color 包装中导入。你可以浏览这个包
装并且改变这个颜色到任何的数值。我选择红色是因为它比较突出。
完整的 autocomplete.java 文件应当看起来像这样:
kListener() {
public void onClick(View v){
changeOption(textView);
}
});
final Button changeButton2 = (Button)
findViewById(R.id.textColorButton);
changeButton2.setOnClickListener(new
Button.OnClickListener() {
public void onClick(View v){
changeOption2(textView);
}
});
public void changeOption(AutoCompleteTextView
text){
if (text.getHeight()==100){
text.setHeight(30);
}
else{
text.setHeight(100);
}
}
public void changeOption2(AutoCompleteTextView
text){
text.setTextColor(Color.RED);
}
}87
package android_programmers_guide.AndroidViews;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.graphics.Color;
public class AutoComplete extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.autocomplete);
ArrayAdapter<String> monthArray = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, Months);
final AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.testAutoComplete);
textView.setAdapter(monthArray);
final Button changeButton = (Button)
findViewById(R.id.autoCompleteButton);
changeButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v){
changeOption(textView);
}
});
final Button changeButton2 = (Button)
findViewById(R.id.textColorButton);
changeButton2.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v){
changeOption2(textView);
}
});
}
static final String[] Months = new String[]{
"January","February","March","April","May","June","July","August",
"September","October","November","December"
};
public void changeOption(AutoCompleteTextView text){
if (text.getHeight()==100){
text.setHeight(30);
}
else{
text.setHeight(100);
}88
增加一个 Intent 过滤器
在运行这个应用程序之前最后一件事就是在 AndroidManifest.xml 文件中设置
intent 过滤器。然后你能从菜单中呼叫 intent 了。Intent 过滤器的代码如下:
这儿是本项目完成的 AndroidManifest.xml 文件:
}
public void changeOption2(AutoCompleteTextView text){
text.setTextColor(Color.RED);
}
}
<activity android:name=".AutoComplete"
android:label="AutoComplete">
<intent-filter>
<action android:name="android.intent.action.MAIN"
/>
<category
android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android=http://schemas.android.com/apk/res/android
package="android_programmers_guide.AndroidViews">
<application android:icon="@drawable/icon">
<activity android:name=".AndroidViews"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AutoComplete"
android:label="AutoComplete">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>当从 select/case 声明中呼叫,这个函数将打开 autocomplete 活动,编辑 select
声明的 case 0 来让它呼叫新的函数:
在 Android 模拟器中运行这个应用程序。当主活动启动后,点击菜单按钮的
AutoComplete 菜单项。点击后应当把你带到 autocomplete 活动。
要测试 AutoCompleteTextView,开始输入单词 January。在你输入几个字母后,
你将会看到 January 出现在文本框中。下一步,点击 Change Layout Button 按
钮,结果会是一个扩展的文本输入框。现在点击 chang Text Color 按钮并且输
入一些文本。
下一节会给你项目中剩下 5 个 Views 的代码支持。
按钮
按钮 第八章(5) (5) (5) (5)
看看下面的代码。这段代码代表了四个文件,AndroidManifest.xml,
Button.xml, testButton.java, 和 AndroidViews.java。增加代码到现存的
AndroidViews 活动中。
警告
如果你没有一开始就跟从本章,你执行代码时可能会遇到麻烦。要确保得到完整
的项目,请从本章的开始开始阅读。
AndroidManifest.xml
</application>
</manifest>
Handling the Intent Call
With AndroidManifest.xml complete, add the following
function to AndroidViews.java:
public void showAutoComplete(){
Intent autocomplete = new Intent(this,
AutoComplete.class);
startActivity(autocomplete);
}
case 0:
showAutoComplete();
return true;

更多相关文章

  1. Android bluetooth介绍(二): android 蓝牙代码架构及其uart 到rfcom
  2. Android系统手机重启与恢复出产设置源代码跟踪
  3. Android中利用SpannableString实现点击同一按钮(Button)不同位置
  4. 网络对讲机C#服务器 Android客户端(三) android客户端代码分析
  5. Android Studio添加so文件并打包到APK的lib文件夹中
  6. android studio打包 so文件

随机推荐

  1. android xml布局中TextView文字居中方法
  2. android手机端保存xml数据
  3. android:MotionEvent
  4. Android连接指定WIFI
  5. Android 点击按钮,文本文字改变
  6. MTK Android P(9.0) userdebug版本执行ad
  7. android 情景模式来电铃声分析(二)
  8. android SQLite 的使用
  9. android 涂鸦
  10. How to Enable USB Debugging Mode on An