package com.lixu.qqjiemian; import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
//欢迎界面
public class WelcomActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.welcom); // 计时器
TimerTask timetask = new TimerTask() { @Override
public void run() {
Intent intent=new Intent(WelcomActivity.this, MainActivity.class);
startActivity(intent); }
};
// 设置时间长短
Timer time = new Timer();
time.schedule(timetask, 3000); }
}
package com.lixu.qqjiemian; import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView; public class MainActivity extends Activity implements android.view.View.OnClickListener {
private TextView xiaoxi;
private TextView lianxiren;
private TextView dongtai; private TextView title; private Fragment xiaoxiFragment;
private Fragment lianxirenFragment;
private Fragment dongtaiFragment; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main); title = (TextView) findViewById(R.id.title); xiaoxi = (TextView) findViewById(R.id.xiaoxi);
lianxiren = (TextView) findViewById(R.id.lianxiren);
dongtai = (TextView) findViewById(R.id.dongtai); xiaoxi.setOnClickListener(this);
lianxiren.setOnClickListener(this);
dongtai.setOnClickListener(this); xiaoxiFragment = new XiaoxiFragment();
lianxirenFragment = new LianxirenFragment();
dongtaiFragment = new DongtaiFragment();
// 初始化的界面设置
choose(1);
title.setText(xiaoxi.getText() + " ");
chooseFragment(xiaoxiFragment); } // 设置点击事件
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.xiaoxi:
choose(1); chooseFragment(xiaoxiFragment); title.setText(xiaoxi.getText() + " "); break;
case R.id.lianxiren: choose(2); chooseFragment(lianxirenFragment); title.setText(lianxiren.getText() + " "); break;
case R.id.dongtai: choose(3); chooseFragment(dongtaiFragment); title.setText(dongtai.getText() + " "); break; default:
break;
} } private void choose(int pos) {
switch (pos) {
case 1:
xiaoxi.setTextColor(Color.BLUE);
xiaoxi.setBackgroundColor(Color.GRAY); lianxiren.setTextColor(Color.GRAY);
lianxiren.setBackgroundColor(Color.WHITE); dongtai.setTextColor(Color.GRAY);
dongtai.setBackgroundColor(Color.WHITE); break;
case 2:
lianxiren.setTextColor(Color.BLUE);
lianxiren.setBackgroundColor(Color.GRAY); xiaoxi.setTextColor(Color.GRAY);
xiaoxi.setBackgroundColor(Color.WHITE); dongtai.setTextColor(Color.GRAY);
dongtai.setBackgroundColor(Color.WHITE);
break; case 3:
dongtai.setTextColor(Color.BLUE);
dongtai.setBackgroundColor(Color.GRAY); lianxiren.setTextColor(Color.GRAY);
lianxiren.setBackgroundColor(Color.WHITE); xiaoxi.setTextColor(Color.GRAY);
xiaoxi.setBackgroundColor(Color.WHITE);
break; default:
break;
}
} // 选择不同的Fragment 的方法
private void chooseFragment(Fragment fragment) { FragmentManager fm = this.getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment, fragment);
// 提交
ft.commit(); } }
package com.lixu.qqjiemian; import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; public class XiaoxiFragment extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(android.R.layout.simple_list_item_1, null); TextView tv = (TextView) view.findViewById(android.R.id.text1);
tv.setText("消息界面");
tv.setBackgroundColor(Color.RED); return view;
}
}
package com.lixu.qqjiemian; import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; public class LianxirenFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(android.R.layout.simple_list_item_1, null); TextView tv = (TextView) view.findViewById(android.R.id.text1);
tv.setText("联系人界面");
tv.setBackgroundColor(Color.GREEN); return view;
}
}
package com.lixu.qqjiemian; import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; public class DongtaiFragment extends Fragment{ @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(android.R.layout.simple_list_item_1, null); TextView tv = (TextView) view.findViewById(android.R.id.text1);
tv.setText("动态界面");
tv.setBackgroundColor(Color.YELLOW); return view;
} }
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:textColor="#f44336"
android:textSize="30sp" /> <FrameLayout
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" > <TextView
android:id="@+id/xiaoxi"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="消息"
android:textSize="15sp" /> <TextView
android:id="@+id/lianxiren"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="联系人"
android:textSize="15sp" /> <TextView
android:id="@+id/dongtai"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="动态"
android:textSize="15sp" />
</LinearLayout> </LinearLayout>
<?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" > <ImageView
android:id="@+id/welcom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/welcome" /> </LinearLayout>
manifest:<activity
android:name=".WelcomActivity"
android:label="@string/app_name"
android:noHistory="true" >
android:noHistory="true"写这个点击回退按钮 不回再回到欢迎界面。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lixu.qqjiemian"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
</activity> <activity
android:name=".WelcomActivity"
android:label="@string/app_name"
android:noHistory="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
运行效果图: