packagecom.example.pwdmanaget;importandroid.app.Activity;importandroid.content.Intent;importandroid.content.SharedPreferences;importandroid.content.res.Resources;importandroid.graphics.Color;importandroid.graphics.drawable.Drawable;importandroid.os.Bundle;importandroid.util.Log;importandroid.view.Gravity;importandroid.view.KeyEvent;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.Button;importandroid.widget.ImageView;importandroid.widget.LinearLayout;importandroid.widget.SimpleAdapter;importandroid.widget.TabHost;importandroid.widget.TextView;importandroid.widget.Toast;importandroid.widget.TabHost.TabSpec;public class MainActivity extendsActivity {private String ACTIVITY_NAME = "B";privateLinearLayout ll;privateButton btn_exit;privateSharedPreferences sp;private intbg_info;private intbc_count;private int[] unSelectedTabIcons ={R.drawable.menu_icon_0_normal,R.drawable.menu_icon_1_normal,R.drawable.menu_icon_2_normal,R.drawable.menu_icon_3_normal};private int[] selectedTabIcons ={R.drawable.menu_icon_0_pressed,R.drawable.menu_icon_1_pressed,R.drawable.menu_icon_2_pressed,R.drawable.menu_icon_3_pressed};private String[] menuText = {"menu1","menu2","menu3"};
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll=(LinearLayout)findViewById(R.id.main_ll);
btn_exit=(Button)findViewById(R.id.btn_exit);
btn_exit.setOnClickListener(newView.OnClickListener() {
@Overridepublic voidonClick(View v) {
finish();//SysApplication.exit();
}
});
sp= getSharedPreferences("PwdManaget", 0);
bg_info= sp.getInt("bg_info", R.drawable.bg);
Resources resources=getResources();
Drawable drawable=resources.getDrawable(bg_info);
ll.setBackgroundDrawable(drawable);//如果没有继承TabActivity时,通过该种方法加载启动tabHost
final TabHost tabHost =(TabHost) findViewById(R.id.tabhost);
tabHost.setup();
TabSpec tabSpec= tabHost.newTabSpec("tab1").setIndicator(getTabHost(0)).setContent(R.id.tab1);
TabSpec tabSpec2= tabHost.newTabSpec("tab2").setIndicator(getTabHost(1)).setContent(R.id.tab2);
TabSpec tabSpec3= tabHost.newTabSpec("tab2").setIndicator(getTabHost(2)).setContent(R.id.tab3);
tabHost.addTab(tabSpec);
tabHost.addTab(tabSpec2);
tabHost.addTab(tabSpec3);
View view= tabHost.getTabWidget().getChildAt(0);
ImageView imageView= (ImageView)view.findViewById(0);
imageView.setImageDrawable(getResources().getDrawable(selectedTabIcons[0]));
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_btn_bg));
tabHost.setOnTabChangedListener(newTabHost.OnTabChangeListener() {
@Overridepublic voidonTabChanged(String tabId) {
updateTab(tabHost);
}
});
}private View getTabHost(inti) {
LinearLayout view= new LinearLayout(this);
view.setOrientation(LinearLayout.VERTICAL);
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.bottom1));
ImageView iv= new ImageView(this);
iv.setId(i);
iv.setImageDrawable(getResources().getDrawable(unSelectedTabIcons[i]));
TextView tv= new TextView(this);
tv.setText(menuText[i]);
tv.setGravity(Gravity.CENTER_HORIZONTAL);
tv.setTextColor(Color.parseColor("#FFFFFF"));
tv.setId(0x0010+i);
view.addView(iv);
view.addView(tv);returnview;
}private voidupdateTab(TabHost tabHost) {int curr =tabHost.getCurrentTab();
View view=tabHost.getTabWidget().getChildAt(curr);
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_btn_bg));
ImageView imageView=(ImageView)view.findViewById(curr);
imageView.setImageDrawable(getResources().getDrawable(selectedTabIcons[curr]));
TextView textView= (TextView)view.findViewById(0x0010+curr);
textView.setTextColor(Color.parseColor("#FFFFFF"));for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {if(i!=curr){
View curr_view=tabHost.getTabWidget().getChildAt(i);
curr_view.setBackgroundDrawable(getResources().getDrawable(R.drawable.bottom1));
ImageView curr_imageView=(ImageView)curr_view.findViewById(i);
curr_imageView.setImageDrawable(getResources().getDrawable(unSelectedTabIcons[i]));
TextView curr_textView= (TextView)curr_view.findViewById(0x0010+i);
curr_textView.setTextColor(Color.parseColor("#E0E0E0"));
}
}
}
@Overrideprotected voidonStart() {super.onStart();
Log.i(ACTIVITY_NAME,"onstart!");
}
@Overrideprotected voidonRestart() {super.onRestart();
Log.i(ACTIVITY_NAME,"onRestart!");
}
@Overrideprotected voidonResume() {super.onResume();
Log.i(ACTIVITY_NAME,"onResume!");
}
@Overrideprotected voidonPause() {super.onPause();
Log.i(ACTIVITY_NAME,"onPause!");
}
@Overrideprotected voidonStop() {super.onStop();
Log.i(ACTIVITY_NAME,"onStop!");
}
@Overrideprotected voidonDestroy() {super.onDestroy();
Log.i(ACTIVITY_NAME,"onDestroy!");
}
@Overridepublic boolean onKeyDown(intkeyCode, KeyEvent event) {if(keyCode ==KeyEvent.KEYCODE_BACK){
Log.i("count", bc_count+"");if(bc_count==0){
Toast.makeText(MainActivity.this, "在按一次退出", Toast.LENGTH_SHORT).show();newThread(){public voidrun() {try{
Thread.sleep(2000);
}catch(InterruptedException e) {
e.printStackTrace();
}finally{
bc_count= 0;
Log.i("count", bc_count+"");
}
};
}.start();
bc_count++;
}else{
finish();
}
}return false;
}
}