80,350
社区成员
发帖
与我相关
我的任务
分享
package com.example.gment;
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.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends FragmentActivity implements OnClickListener {
private Button btn;
private Fragment current;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(this);
current=new Fragment();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
Fragment1 fragment1=new Fragment1();
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
if(current==fragment1)
break;
else{
transaction.hide(current);
transaction.show(fragment1);
transaction.commit();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
public class MainActivity extends FragmentActivity implements OnClickListener {
private Button btn;
private TextView textView5;
private Fragment current;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.btn);
textView5=(TextView)findViewById(R.id.textView5);
btn.setOnClickListener(this);
current=new Fragment();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
try {
Fragment1 fragment1=new Fragment1();
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
if(current==fragment1)
break;
if(!fragment1.isAdded())
{
transaction.hide(current);
transaction.add(R.id.cont, fragment1);
transaction.commit();
}
else{
transaction.hide(current);
transaction.show(fragment1);
transaction.commit();
}
current=fragment1;
break;
}
catch(Exception e)
{
Log.d("报错", e.toString());
textView5.setText(e.toString());
}
}
}}
public class Fragment1 extends android.support.v4.app.Fragment {
private Bitmap bitmap;
private Button btn;
private Button btn1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v=inflater.inflate(R.layout.y1, container, false); //让fragment关联布局文件
//intent = new Intent(getActivity(),Shoppingtrolley.class);//转跳到Shoppingtrolley
btn=(Button)v.findViewById(R.id.btn11);//第一个按钮
btn1=(Button)v.findViewById(R.id.btn12);//第二个按钮
bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.abc_ab_bottom_solid_dark_holo);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent1 = new Intent(getActivity(),Shoppingtrolley.class);
intent1.putExtra("bitmap",bitmap);//传一张图片
intent1.putExtra("整数", 38);//传一个int型的数值
startActivity(intent1);//启动转跳
Toast.makeText(getActivity(),"提交至购物车",0).show();
}
});
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent2 = new Intent(getActivity(),Shoppingtrolley.class);
startActivity(intent2);//启动转跳
Toast.makeText(getActivity(),"提交至购物车",0).show();
}
});
}
}
<LinearLayout 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.gment.MainActivity"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/cont"
android:layout_width="match_parent"
android:layout_height="410dp">
</FrameLayout>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
package com.example.gment;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class Fragment1 extends Fragment {
private Bitmap bitmap;
private Button btn;
private Button btn1;
Intent intent;
@Override
public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
View v=inflater.inflate(R.layout.y1, container, false); //让fragment关联布局文件
intent = new Intent(getActivity(),Shoppingtrolley.class);//转跳到Shoppingtrolley
btn=(Button)v.findViewById(R.id.btn11);//第一个按钮
btn1=(Button)v.findViewById(R.id.btn12);//第二个按钮
bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.abc_ab_bottom_solid_dark_holo);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
intent.putExtra("bitmap",bitmap);//传一张图片
intent.putExtra("整数", 38);//传一个int型的数值
startActivity(intent);//启动转跳
Toast.makeText(getActivity(),"提交至购物车",0).show();
}
});
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(intent);//启动转跳
Toast.makeText(getActivity(),"提交至购物车",0).show();
}
});
}
}
package com.example.gment;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class Shoppingtrolley extends Activity {
private Bitmap bitmap,bitmap1;
ImageView imageview,imageview1;
TextView tv;
int price;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shoppingtrolley);
imageview=(ImageView)findViewById(R.id.imageView1);
tv=(TextView) findViewById(R.id.textView2);
Intent intent=getIntent();
if(intent!=null)
{
bitmap=intent.getParcelableExtra("bitmap");
imageview.setImageBitmap(bitmap);
//price=getIntent().getExtras().getInt("整数");
price=intent.getIntExtra("整数", 0);
tv.setText("¥"+price+"");
}
}
}
Class<Fragment> c1 = Fragment.class;
Class<Fragment1> c2 = Fragment1.class;
if(c1.toString().equals(c2.toString())){
}