package com.example.test;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.Toast;
import com.example.fragment.Fragment1;
import com.example.fragment.Fragment2;
import com.example.fragment.Fragment3;
import com.example.fragment.Fragment4;
public class HomeActivity extends FragmentActivity implements OnClickListener {
private RadioButton text1;
private RadioButton text2;
private RadioButton text3;
private RadioButton text4;
private FragmentManager manager;
private Fragment f1;
private Fragment2 f2;
private Fragment3 f3;
private Fragment4 f4;
private int flag;
private long mExitTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_home);
text1=(RadioButton) findViewById(R.id.text1);
text2=(RadioButton) findViewById(R.id.text2);
text3=(RadioButton) findViewById(R.id.text3);
text4=(RadioButton) findViewById(R.id.text4);
text1.setOnClickListener(this);
text2.setOnClickListener(this);
text3.setOnClickListener(this);
text4.setOnClickListener(this);
flag = getIntent().getIntExtra("shop_num", 0);
getfragment();
changefragment("f1");
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if ((System.currentTimeMillis() - mExitTime) > 2000) {
Object mHelperUtils;
Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
mExitTime = System.currentTimeMillis();
} else {
finish();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
private void changefragment(String tag) {
FragmentTransaction transaction=manager.beginTransaction();
if("f1".equals(tag)){
transaction.show(f1);
transaction.hide(f2);
transaction.hide(f3);
transaction.hide(f4);
}else if("f2".equals(tag)){
transaction.show(f2);
transaction.hide(f1);
transaction.hide(f3);
transaction.hide(f4);
}else if("f3".equals(tag)){
transaction.show(f3);
transaction.hide(f1);
transaction.hide(f2);
transaction.hide(f4);
}else if("f4".equals(tag)){
transaction.show(f4);
transaction.hide(f2);
transaction.hide(f3);
transaction.hide(f1);
}
transaction.commit();
}
private void getfragment() {
manager=getSupportFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
//ʵ��fragment
f1=new Fragment1();
f2=new Fragment2();
f3=new Fragment3();
f4=new Fragment4();
transaction.add(R.id.frame, f1,"f1");
transaction.add(R.id.frame, f2,"f2");
transaction.add(R.id.frame, f3,"f3");
transaction.add(R.id.frame, f4,"f4");
transaction.commit();
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.text1:
changefragment("f1");
break;
case R.id.text2:
changefragment("f2");
break;
case R.id.text3:
if(flag==5){
changefragment("f3");
}else{
Intent intent=new Intent(HomeActivity.this,LoginActivity.class);
startActivity(intent);
}
break;
case R.id.text4:
changefragment("f4");
break;
default:
break;
}
}
}
fragment的点击切换页面
最新推荐文章于 2020-06-30 20:38:02 发布