Android开发从入门到精通(8) _4

本节指导如何在Android应用中创建突出AutoCompleteTextView的活动,利用其自动完成功能优化用户体验,通过创建自定义布局、编写Java代码及配置Intent过滤器实现功能。包括设置布局、初始化AutoCompleteTextView、绑定事件处理器及完成Intent配置等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

为AutoComplete创建一个活动 第八章(4)

  在本节中,你将创建一个突出AutoCompleteTextView的活动。AutoCompleteTextView对你的有应用程序来说可以成为一个非常有力的工具。特别是对于Android主屏幕有限的空间来说。

AutoCompleteTextView,正如这个名字所说,是修改后的TextView,而它可以参考到可用的单词或者短语并自动完成输入。这样的Views是在移动应用程序里是非常有用的当你不想花费大量的空间到一个ListView,或者你想要加速你输入文本的过程。

    要开始为AutoCompleteTextView创建活动,你需要为布局增加一个新的.xml文件,为代码增加一个.java文件,并且一个Intent过滤器来处理呼叫。

提示

创建这些条目的过程出现在本章“构造活动”一节中。创建下面项目的部分时可以参考那个部分。

创建一个autocomplete.xml文件 
在你的AndroidViews项目中创建一个新的.xml文件,并命名为autocomplete.xml。记住文件名必须用小写。这个文件应当出现在layout文件夹。双击这个文件来编辑它。这个文件会控制AutoCompleteTextView活动的布局,所以你需要在布局中有一个AutoCompleteTextView。增加过AutoCompleteTextView的XML文件应当如下:

<AutoCompleteTextView android:id="@+id/testAutoComplete" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"/>

你已经在.xml文件中创建了几个Views了,所以你应当熟悉这个格式。对于AutoCompleteTextView,没什么特别之处。你设定id到testAutoComplete,还有相应的宽度和高度到fill_parent和wrap_content,还应该为两个按钮增加布局。这些按钮将被用于你将改变的属性控制。命名按钮为autoCompleteButton和textColorButton,如下:

 

<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"/>


   有了新增的三个View布局,你完成的autocomplete.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 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>

创建一个autocomplete.java文件 
    跟从本章“创建一个新的java文件”的介绍。第一件要做的事就是为你的Views输入包装。在这个活动中,使用了两个Views,AutoCompleteTextView 和按钮。你还需要设置颜色和一个ArrayAdapter。因此,输入下面包装到活动中:

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;

注意

现在你可能不知道它们的用途,先加入它们吧,我会解释的。

为AutoCOmplete类增加初始的结构到autocomplete.java文件中:

public class AutoComplete extends Activity { 
@Override 
public void onCreate(Bundle icicle) { 
super.onCreate(icicle); 

}

这个类给了你建造活动其它部分的基础。这个活动的所有功能将会被围绕这个类建造。第一件要做的事就是从autocomplete.xml中装载布局:

setContentView(R.layout.autocomplete);

对于本例,将创建AutoComplete TextView,所以它包含一年中的月份。当一个用户在框中输入,它会猜测那个月份用户试图去输入。假定AutoComplete TextView将包含月份的清单,你需要来创建一个可以被赋值到AutoCompleteTextView的清单。

创建字符串数组并赋值月份数值到其中:

static final String[] Months = new String[]{ 
"January","February","March","April","May","June","July","August", 
"September","October","November","December" 
};

下一个任务是复制这个字符串到AutoCompleteTextView。到目前为止,你已经创建了一些Views了。所以,创建AutoCompleteTextView的代码看上去应该很熟悉。你之前没看到过的就是把字符串赋值给View:

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);

在第一行,拿去创建的字符串数组并且复制到一个名为monthArray的ArrayAdapter。下一步,你通过在.xml文件中定位来例示AutoCompleteTextView。最后,使用setAdapter()方法来赋值monthArray ArrayAdapter 到AutoCompleteTextView中。

下一个零星的代码例示那两个按钮。与上一章的代码相同。唯一和你所写代码不同的是你正在呼叫两个函数,changeOption 和 changeOption2,而这些,你还没有创建呢。

注意

你在传递AutoCompleteTextView 到函数呼叫。当你创建函数还需要创建参数。

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); 

});

被这些按钮呼叫的函数将被用于在AutoComplete TextView改变布局属性。这两个我选择来修改的属性(通过两个按钮)是布局的高度和文本的颜色。你将设置一个按钮来改变AutoCompleteTextView的布局高度,从30到100并且返回。另一个按钮将改变View内文本的为红色。

函数changeOption()会改变AutoCompleteTextView的布局高度。代码非常的简单:

public void changeOption(AutoCompleteTextView text){ 
if (text.getHeight()==100){ 
text.setHeight(30); 

else{ 
text.setHeight(100); 

}

在这个函数中你要做的就是检查当前AutoCompleteTextView的高度。如果高度是100,把它设为30,否则设为100。

changeOption2()函数也简单:

public void changeOption2(AutoCompleteTextView text){ 
text.setTextColor(Color.RED); 

}

这个函数简单的把AutoCompleteTextView的文本颜色设为Color.RED。

Color.RED的数值从android.graphics.Color包装中导入。你可以浏览这个包装并且改变这个颜色到任何的数值。我选择红色是因为它比较突出。

完整的autocomplete.java 文件应当看起来像这样:

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); 


public void changeOption2(AutoCompleteTextView text){ 
text.setTextColor(Color.RED); 

增加一个Intent过滤器 
在运行这个应用程序之前最后一件事就是在AndroidManifest.xml文件中设置intent过滤器。然后你能从菜单中呼叫intent了。Intent过滤器的代码如下:

<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>

这儿是本项目完成的AndroidManifest.xml文件:

<?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> 
</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); 
}

当从select/case声明中呼叫,这个函数将打开autocomplete活动,编辑select声明的case 0来让它呼叫新的函数:

case 0: 
showAutoComplete(); 
return true;

在Android模拟器中运行这个应用程序。当主活动启动后,点击菜单按钮的AutoComplete菜单项。点击后应当把你带到autocomplete活动。

要测试AutoCompleteTextView,开始输入单词January。在你输入几个字母后,你将会看到January出现在文本框中。下一步,点击Change Layout Button按钮,结果会是一个扩展的文本输入框。现在点击chang Text Color按钮并且输入一些文本。

下一节会给你项目中剩下5个Views的代码支持。

更多信息请查看 http://www.javady.com/index.php/category/thread

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值