Fragment子类关系图:
SearchView的使用
TabHost的使用注意事项
ScrollView的功能和用法
SearchView的使用:
SearchView是搜索框组件,可以让用户在文本框中输入文字,并允许通过该监听器监控用户输入,当用户输入完成后提交到搜索时,也可以通过监听器执行实际的搜索`。`
常用方法如下:
// 设置该SearchView默认是否自动缩小为图标
sv.setIconifiedByDefault(true);
// 设置该SearchView显示搜索按钮
sv.setSubmitButtonEnabled(true);
// 设置该SearchView内默认显示的提示文本
sv.setQueryHint("查找");
// 为该SearchView组件设置事件监听器
sv.setOnQueryTextListener(new OnQueryTextListener()){}
main.xml的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 定义一个SearchView -->
<SearchView
android:id="@+id/sv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!-- 为SearchView定义自动完成的ListView-->
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
Activity代码区;
public class MainActivity extends Activity
{
private SearchView sv;
private ListView lv;
// 自动完成的列表
private final String[] mStrings = { "aserbao", "aaaaaa", "cccccc" };
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mStrings));
// 设置ListView启用过滤
lv.setTextFilterEnabled(true);
sv = (SearchView) findViewById(R.id.sv);
// 设置该SearchView默认是否自动缩小为图标
sv.setIconifiedByDefault(true);
// 设置该SearchView显示搜索按钮
sv.setSubmitButtonEnabled(true);
// 设置该SearchView内默认显示的提示文本
sv.setQueryHint("查找");
// 为该SearchView组件设置事件监听器
sv.setOnQueryTextListener(new OnQueryTextListener()
{
// 用户输入字符时激发该方法
@Override
public boolean onQueryTextChange(String newText)
{
// 如果newText不是长度为0的字符串
if (TextUtils.isEmpty(newText))
{
// 清除ListView的过滤
lv.clearTextFilter();
}
else
{
// 使用用户输入的内容对ListView的列表项进行过滤
lv.setFilterText(newText);
}
return true;
}
// 单击搜索按钮时激发该方法
@Override
public boolean onQueryTextSubmit(String query)
{
// 实际应用中应该在该方法内执行实际查询
// 此处仅使用Toast显示用户输入的查询内容
Toast.makeText(MainActivity.this, "您的选择是:" + query
, Toast.LENGTH_SHORT).show();
return false;
}
});
}
}
TabHost的使用注意事项:
与TabHost结合的组件还有:
TabWidget:代表选项卡的标题条
TabSpec:代表选项卡的一个Tab页面
TabHost只是一个简单的容器,它提供下面两个方法来创建,添加标签页:
newTabSpec(String tag):创建选项卡。
addTab(TabHost,TabSpec tabSpec):添加选项卡。
使用TabHost的一般步骤如下:
1:定义TabHost组件,并添加内容
2:Activity继承自TabActivity
3:通过TanActivity的getHostTab()方法来获取TabHost对象
4:通过TabHost对象的方法来创建,添加选项卡
ID要求:
TabHost的ID应该是:@android:id/tabhost
TabWeight的ID应该是:@android:id/tabs
FrameLayout的ID应该是:@android:id/tabcontent
main.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 定义第一个标签页的内容 -->
<LinearLayout
android:id="@+id/tab01"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="aserbao - 1142803753"
android:textSize="11pt" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="aserbao - 9561623456"
android:textSize="11pt" />
</LinearLayout>
<!-- 定义第二个标签页的内容 -->
<LinearLayout
android:id="@+id/tab02"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="eb - 29263792917"
android:textSize="11pt" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="eb - 29263654879"
android:textSize="11pt" />
</LinearLayout>
<!-- 定义第三个标签页的内容 -->
<LinearLayout
android:id="@+id/tab03"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="11pt">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="321546 - 3214569879"
android:textSize="11pt" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10086 - 10086"
android:textSize="11pt" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
Activity代码如下:
public class MainActivity extends TabActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
// 获取该Activity里面的TabHost组件
TabHost tabHost = getTabHost();
// 创建第一个Tab页
TabHost.TabSpec tab1 = tabHost.newTabSpec("tab1")
.setIndicator("已接电话") // 设置标题
.setContent(R.id.tab01); //设置内容
// 添加第一个标签页
tabHost.addTab(tab1);
TabHost.TabSpec tab2 = tabHost.newTabSpec("tab2")
// 在标签标题上放置图标
.setIndicator("呼出电话", getResources()
.getDrawable(R.drawable.ic_launcher))
.setContent(R.id.tab02);
// 添加第二个标签页
tabHost.addTab(tab2);
TabHost.TabSpec tab3 = tabHost.newTabSpec("tab3")
.setIndicator("未接电话")
.setContent(R.id.tab03);
// 添加第三个标签页
tabHost.addTab(tab3);
}
}
最新的Android平台已不再推荐使用TabActivity,而是用Fragment代替。
ScrollView的功能和用法:
ScrollView和HorizontalScrollView的功能基本相似:
代码区:
main.xml的代码:
这里添加了大量的TextView只为了测试上下左右滑动的效果,实测中可以去掉一部分.
<?xml version="1.0" encoding="utf-8"?>
<!-- 定义ScrollView,为里面的组件添加垂直滚动条 -->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 定义HorizontalScrollView,为里面的组件添加水平滚动条 -->
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--省略多个Text View组件-->
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="aserbaoaserbaoaserbaoaserbaoaserbaoaserbaoaserbaoaserbaoaserbaoaserbaoaserbaoaserbao"
android:textSize="30dp" />
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
scrollView与listView的冲突,是一个经常让大家头疼的问题,也是经常困扰我的问题,之后博主会就这个问题写一篇详细的博客!如果各位有什么Android方面好的意见或者建议,问题,可以留言,看到在第一时间回复,不会的可以加个好友一起探讨学习!