1 package com.lixu.qqjiemian; 2 3 import java.util.Timer; 4 import java.util.TimerTask; 5 import android.app.Activity; 6 import android.content.Intent; 7 import android.os.Bundle; 8 import android.view.Window; 9 //欢迎界面10 public class WelcomActivity extends Activity {11 @Override12 protected void onCreate(Bundle savedInstanceState) {13 super.onCreate(savedInstanceState);14 requestWindowFeature(Window.FEATURE_NO_TITLE);15 setContentView(R.layout.welcom);16 17 18 // 计时器19 TimerTask timetask = new TimerTask() {20 21 @Override22 public void run() {23 Intent intent=new Intent(WelcomActivity.this, MainActivity.class);24 startActivity(intent);25 26 }27 };28 // 设置时间长短29 Timer time = new Timer();30 time.schedule(timetask, 3000);31 32 }33 }
1 package com.lixu.qqjiemian; 2 3 import android.app.Activity; 4 import android.app.Fragment; 5 import android.app.FragmentManager; 6 import android.app.FragmentTransaction; 7 import android.graphics.Color; 8 import android.os.Bundle; 9 import android.view.View; 10 import android.view.Window; 11 import android.widget.TextView; 12 13 public class MainActivity extends Activity implements android.view.View.OnClickListener { 14 private TextView xiaoxi; 15 private TextView lianxiren; 16 private TextView dongtai; 17 18 private TextView title; 19 20 private Fragment xiaoxiFragment; 21 private Fragment lianxirenFragment; 22 private Fragment dongtaiFragment; 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 28 requestWindowFeature(Window.FEATURE_NO_TITLE); 29 setContentView(R.layout.activity_main); 30 31 title = (TextView) findViewById(R.id.title); 32 33 xiaoxi = (TextView) findViewById(R.id.xiaoxi); 34 lianxiren = (TextView) findViewById(R.id.lianxiren); 35 dongtai = (TextView) findViewById(R.id.dongtai); 36 37 xiaoxi.setOnClickListener(this); 38 lianxiren.setOnClickListener(this); 39 dongtai.setOnClickListener(this); 40 41 xiaoxiFragment = new XiaoxiFragment(); 42 lianxirenFragment = new LianxirenFragment(); 43 dongtaiFragment = new DongtaiFragment(); 44 // 初始化的界面设置 45 choose(1); 46 title.setText(xiaoxi.getText() + " "); 47 chooseFragment(xiaoxiFragment); 48 49 } 50 51 // 设置点击事件 52 @Override 53 public void onClick(View v) { 54 switch (v.getId()) { 55 case R.id.xiaoxi: 56 choose(1); 57 58 chooseFragment(xiaoxiFragment); 59 60 title.setText(xiaoxi.getText() + " "); 61 62 break; 63 case R.id.lianxiren: 64 65 choose(2); 66 67 chooseFragment(lianxirenFragment); 68 69 title.setText(lianxiren.getText() + " "); 70 71 break; 72 case R.id.dongtai: 73 74 choose(3); 75 76 chooseFragment(dongtaiFragment); 77 78 title.setText(dongtai.getText() + " "); 79 80 break; 81 82 default: 83 break; 84 } 85 86 } 87 88 private void choose(int pos) { 89 switch (pos) { 90 case 1: 91 xiaoxi.setTextColor(Color.BLUE); 92 xiaoxi.setBackgroundColor(Color.GRAY); 93 94 lianxiren.setTextColor(Color.GRAY); 95 lianxiren.setBackgroundColor(Color.WHITE); 96 97 dongtai.setTextColor(Color.GRAY); 98 dongtai.setBackgroundColor(Color.WHITE); 99 100 break;101 case 2:102 lianxiren.setTextColor(Color.BLUE);103 lianxiren.setBackgroundColor(Color.GRAY);104 105 xiaoxi.setTextColor(Color.GRAY);106 xiaoxi.setBackgroundColor(Color.WHITE);107 108 dongtai.setTextColor(Color.GRAY);109 dongtai.setBackgroundColor(Color.WHITE);110 break;111 112 case 3:113 dongtai.setTextColor(Color.BLUE);114 dongtai.setBackgroundColor(Color.GRAY);115 116 lianxiren.setTextColor(Color.GRAY);117 lianxiren.setBackgroundColor(Color.WHITE);118 119 xiaoxi.setTextColor(Color.GRAY);120 xiaoxi.setBackgroundColor(Color.WHITE);121 break;122 123 default:124 break;125 }126 }127 128 // 选择不同的Fragment 的方法129 private void chooseFragment(Fragment fragment) {130 131 FragmentManager fm = this.getFragmentManager();132 FragmentTransaction ft = fm.beginTransaction();133 ft.replace(R.id.fragment, fragment);134 // 提交135 ft.commit();136 137 }138 139 }
1 package com.lixu.qqjiemian; 2 3 import android.app.Fragment; 4 import android.graphics.Color; 5 import android.os.Bundle; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.TextView;10 11 public class XiaoxiFragment extends Fragment {12 13 @Override14 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {15 16 View view = inflater.inflate(android.R.layout.simple_list_item_1, null);17 18 TextView tv = (TextView) view.findViewById(android.R.id.text1);19 tv.setText("消息界面");20 tv.setBackgroundColor(Color.RED);21 22 return view;23 }24 }
1 package com.lixu.qqjiemian; 2 3 import android.app.Fragment; 4 import android.graphics.Color; 5 import android.os.Bundle; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.TextView;10 11 public class LianxirenFragment extends Fragment {12 @Override13 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {14 15 View view = inflater.inflate(android.R.layout.simple_list_item_1, null);16 17 TextView tv = (TextView) view.findViewById(android.R.id.text1);18 tv.setText("联系人界面");19 tv.setBackgroundColor(Color.GREEN);20 21 return view;22 }23 }
1 package com.lixu.qqjiemian; 2 3 import android.app.Fragment; 4 import android.graphics.Color; 5 import android.os.Bundle; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.TextView;10 11 public class DongtaiFragment extends Fragment{12 13 @Override14 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {15 16 View view = inflater.inflate(android.R.layout.simple_list_item_1, null);17 18 TextView tv = (TextView) view.findViewById(android.R.id.text1);19 tv.setText("动态界面");20 tv.setBackgroundColor(Color.YELLOW);21 22 return view;23 }24 25 }
xml:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:id="@+id/LinearLayout1" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" > 7 8 <TextView 9 android:id="@+id/title"10 android:layout_width="match_parent"11 android:layout_height="wrap_content"12 android:gravity="center"13 android:layout_weight="1"14 android:textColor="#f44336"15 android:textSize="30sp" />16 17 <FrameLayout18 android:id="@+id/fragment"19 android:layout_width="match_parent"20 android:layout_height="wrap_content"21 android:layout_weight="10" />22 23 <LinearLayout24 android:layout_width="match_parent"25 android:layout_height="wrap_content"26 android:layout_weight="1"27 android:orientation="horizontal" >28 29 <TextView30 android:id="@+id/xiaoxi"31 android:layout_width="wrap_content"32 android:layout_height="match_parent"33 android:layout_weight="1"34 android:gravity="center"35 android:text="消息"36 android:textSize="15sp" />37 38 <TextView39 android:id="@+id/lianxiren"40 android:layout_width="wrap_content"41 android:layout_height="match_parent"42 android:layout_weight="1"43 android:gravity="center"44 android:text="联系人"45 android:textSize="15sp" />46 47 <TextView48 android:id="@+id/dongtai"49 android:layout_width="wrap_content"50 android:layout_height="match_parent"51 android:layout_weight="1"52 android:gravity="center"53 android:text="动态"54 android:textSize="15sp" />55 </LinearLayout>56 57 </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"写这个点击回退按钮 不回再回到欢迎界面。
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.lixu.qqjiemian" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="19" 9 android:targetSdkVersion="19" />10 11 <application12 android:allowBackup="true"13 android:icon="@drawable/ic_launcher"14 android:label="@string/app_name"15 android:theme="@style/AppTheme" >16 <activity17 android:name=".MainActivity"18 android:label="@string/app_name" >19 </activity>20 21 <activity22 android:name=".WelcomActivity"23 android:label="@string/app_name"24 android:noHistory="true" >25 <intent-filter>26 <action android:name="android.intent.action.MAIN" />27 28 <category android:name="android.intent.category.LAUNCHER" />29 </intent-filter>30 </activity>31 </application>32 33 </manifest>
运行效果图: