微信综合练习
第一部分.用Fragment制作各界面
① 创建Fragment切换按钮
<LinearLayout
android:id="@+id/lin_menu"
android:gravity="center_vertical"
android:layout_alignParentBottom="true"
android:background="@drawable/menu_bg"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:src="@drawable/weixin_pressed"
android:background="@null"
android:id="@+id/iv_weixin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<ImageButton
android:src="@drawable/contact_list_normal"
android:background="@null"
android:id="@+id/iv_contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<ImageButton
android:src="@drawable/find_normal"
android:background="@null"
android:id="@+id/iv_find"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<ImageButton
android:src="@drawable/profile_normal"
android:background="@null"
android:id="@+id/iv_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
② 创建各个Fragment界面
1.微信界面Fragment
核心代码
package com.example.day59;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class WeixinFragment extends Fragment {
public WeixinFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_weixin, container, false);
}
}
布局代码
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.day59.WeixinFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="微信" />
</FrameLayout>
2.联系人Fragment
核心代码
public class ContactFragment extends Fragment {
public ContactFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_contact, container, false);
}
布局文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.day59.ContactFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="联系人" />
</FrameLayout>
3.发现Fragment
核心代码
public class FindFragment extends Fragment {
public FindFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_find, container, false);
}
}
布局文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.day59.FindFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="发现" />
</FrameLayout>
4.我的Fragment
核心代码
public class MeFragment extends Fragment {
public MeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_me, container, false);
}
}
布局文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.day59.MeFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
③ 需要在MainActivity中对各个按钮设置点击事件
1.让类继承View.OnClickListener
public class MainActivity extends AppCompatActivity implements View.OnClickListener
2.在类中实现点击事件的方法
//设置点击事件
iv_weixin.setOnClickListener(this);
iv_contact.setOnClickListener(this);
iv_find.setOnClickListener(this);
iv_me.setOnClickListener(this);
3.实现点击事件的监听,并设置指定的操作
public void onClick(View v) {
//当按钮点击时默认更新一次按钮颜色
change();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
switch (v.getId()){
case R.id.iv_contact:
Log.e(getClass().getSimpleName(),"点击了");
if (contactFragment==null){
contactFragment = new ContactFragment();
}
transaction.replace(R.id.framelayout_contain,contactFragment);
iv_contact.setImageResource(R.drawable.contact_list_pressed);
break;
case R.id.iv_find:
if (findFragment==null){
findFragment = new FindFragment();
}
transaction.replace(R.id.framelayout_contain,findFragment);
iv_find.setImageResource(R.drawable.find_pressed);
break;
case R.id.iv_me:
if (meFragment==null){
meFragment = new MeFragment();
}
transaction.replace(R.id.framelayout_contain,meFragment);
iv_me.setImageResource(R.drawable.profile_pressed);
break;
case R.id.iv_weixin:
if (weixinFragment==null){
weixinFragment = new WeixinFragment();
}
transaction.replace(R.id.framelayout_contain,weixinFragment);
iv_weixin.setImageResource(R.drawable.weixin_pressed);
break;
}
transaction.commit();
}
4.在应用第一次加载时默认加载一次Froament
//当按钮点击时默认更新一次按钮颜色
change();
//当按钮点击时,按钮颜色更新
public void change(){
iv_contact.setImageResource(R.drawable.contact_list_normal);
iv_find.setImageResource(R.drawable.find_normal);
iv_me.setImageResource(R.drawable.profile_normal);
iv_weixin.setImageResource(R.drawable.weixin_normal);
}
5.在最后一定要提交一次,否则效果无法展示
transaction.commit();
第二部分.菜单制作
① 创建一个menu资源文件
<?xml version="1.0" encoding="utf-8"?>
<item
android:title="选项一"
android:orderInCategory="100"
android:id="@+id/choice1"
/>
<item
android:title="选项二"
android:orderInCategory="90"
android:id="@+id/choice2"
/>
<item
android:title="内容自动补全"
android:orderInCategory="80"
android:id="@+id/choice3"
/>
</menu>
② 在MainActivity中创建菜单,并把菜单通过inflater导入到界面中
//创建一个菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
第三部分 自动补全提示
① 创建一个XML文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.day59.SearchActivity">
<AutoCompleteTextView
android:id="@+id/act_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入搜索内容"/>
</RelativeLayout>
② 创建一个布局
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="25sp"
android:textColor="#000000">
</TextView>
③ 在MainActivity中声明并导入数据
//初始化控件
autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.act_search);
String[]date = {"zhangsan","lisi","wangwu","zhaoliu"};
ArrayAdapter<String>adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.item,date);
autoCompleteTextView.setAdapter(adapter);