标签:
2、针对Focused的TextView跑马灯(文字较多一行无法显示)效果
针对单个TextView的跑马灯效果,可直接在TextView控件参数中添加三个属性:
android:singleLine="true"(一行显示会有省略号)
android:ellipsize="marquee"(一行显示无省略号)
android:focusable="true"
android:focusableInTouchMode="true"(最后两条结合使得单个TextView可实现效果)
针对两个或以上TextView或复杂结构的“跑马灯效果”实现需借助自定义控件,具体方法如下:
首先创建两个不同的TextView控件,并分别标明其各自的Id,然后新建一个类,该类继承自TextView,
在类方法里面将构造函数方法全部实现(通过右击找到Generate中的Constructor,依次双击全部构造类)
全部构造方法实现之后,另外添加public boolean isFocused(){return true;},最后再返回XML文件
中,将TextView控件全部换成自定义的 <com.sub.coder.MarqueeText ...>即可实现双跑马灯效果!
3、如何实现对AutoCompleteTextView(根据输入字符自动补全,类百度搜素框)的使用
第一步,拖放AutoCompleteTextView 控件,修改hint=“请输入您想要搜索的内容”并在.java文件中进行控件初始化操作
第二步,新建适配器(Adapter)并初始化数据源数组(用于匹配搜索内容)
第三步,将AutoCompleteTextView 控件与适配器(Adapter)进行绑定操作即可
另外注意,通过android:completionThreshold="2"控件属性设置可修改开始匹配时字符串个数
具体实现代码:package com.sub.coder;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
private AutoCompleteTextView acTextView;
private String[] res = {"beijing1","beijing2","bejing3","shanghai1","shanghai2"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
acTextView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,res);
acTextView.setAdapter(adapter);
}
}
4、如何实现MultiAutoCompleteTextView(类似邮箱收件人的多重匹配功能),同上,需多设分割符代码,具体如下:
package com.sub.coder;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
private AutoCompleteTextView acTextView;
private MultiAutoCompleteTextView macTextView;
private String[] res = {"beijing1","beijing2","bejing3","shanghai1","shanghai2"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
acTextView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,res);
acTextView.setAdapter(adapter);
macTextView = (MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextView);
macTextView.setAdapter(adapter);
macTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
5、利用ToggleButton按钮和ImageView控件实现开关效果:
首先在XML文件中进行ToggleButton按钮和ImageView控件的建立,然后在.java中初始化他们,在本类中实现.OnCheckedChangeListener的接口,
然后在初始化完成之后进行一个方法监听 tb.setOnCheckedChangeListener(this);最后在实现接口时构建的方法中利用一个三目运算符进行当前
按钮状态的判断以及对应状态的图片切换。
package com.sub.coder;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
public class MyActivity extends Activity implements CompoundButton.OnCheckedChangeListener{
private ToggleButton tb;
private ImageView img;
private String[] res = {"beijing1","beijing2","bejing3","shanghai1","shanghai2"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tb = (ToggleButton)findViewById(R.id.toggleButton);
img = (ImageView)findViewById(R.id.imageView);
tb.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
img.setBackgroundResource(isChecked?R.drawable.on:R.drawable.off);
}
}
标签:
原文地址:http://www.cnblogs.com/Coding-YZZ/p/4690695.html