Activity

public class TiaozhuanActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tiaozhuan);
Intent intent = getIntent();
TextView tvshow=(TextView)findViewById(R.id.show);
EditText etshuru=(EditText)findViewById(R.id.shuru);
String name=intent.getStringExtra("name");
tvshow.setText(name);
}
public void onClick(View view){
switch (view.getId()){
case R.id.button1:
case R.id.button2:
enter();
break;
}
}

private void enter() {
EditText etshuru=(EditText)findViewById(R.id.shuru);
String name =etshuru.getText().toString();
Intent intent=new Intent(this,JiemianActivity.class);
intent.putExtra("name",name+"同学:学习Android有没有信心?");
startActivity(intent);

}
}

 

 

 

 


package hello.jmtiaozhuan;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class JiemianActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jiemian2);
Intent intent = getIntent();
TextView tvshow=(TextView)findViewById(R.id.show);
String name=intent.getStringExtra("name");
tvshow.setText(name);
}
public void onClick(View view){
switch (view.getId()){
case R.id.button3:
ruturn();
break;
case R.id.button4:
ruturn2();
break;
}
}

<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical"
tools:context="com.example.a15083.fragmentandactivity.FragmentActivity">

<LinearLayout
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="1">

</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">

<Button
android:id="@+id/bt_show"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="@string/bt_show"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:onClick="onClick"
/>
</LinearLayout>

</LinearLayout>

<FrameLayout 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.a15083.fragmentandactivity.FirstFragment">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_light"
android:gravity="center"
android:textSize="25sp"
android:text="@string/first_fragment" />

</FrameLayout>

package com.example.a15083.fragmentandactivity;


import android.app.Fragment;
import android.os.Bundle;;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/
 A simple {@link Fragment} subclass.
/
public class FirstFragment extends Fragment {

public FirstFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first, container, false);
}

}

package com.example.a15083.fragmentandactivity;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

public class FragmentActivity extends AppCompatActivity implements View.OnClickListener{
FirstFragment firstFragment;
SecondFragment secondFragment;
private boolean a=true; //a的作用就是来判断第二次点击按钮时是否切换fragment
boolean e=false; //e的作用是用来在第一个fragment点击返回时就执行退出程序操作

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);

FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
firstFragment = new FirstFragment();
transaction.add(R.id.show,firstFragment);
transaction.commit();
}

@Override
public void onClick(View view) {
if(view.getId()==R.id.bt_show){
e=true; //将e的值设置为true,使在点击返回键时能够切换fragment
if(a){
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
if (secondFragment == null){
secondFragment = new SecondFragment();
transaction.replace(R.id.show,secondFragment);
transaction.commit();
a=false; //设置a为false,使在第二次以后点击按钮能够弹出提示
} else{
transaction.replace(R.id.show,secondFragment);
transaction.commit();
a=false; //设置a为false,使在第二次以后点击按钮能够弹出提示
}
}else{
Toast.makeText(this,"This is second fragment",Toast.LENGTH_SHORT).show();
}
}
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getKeyCode()==KeyEvent.KEYCODE_BACK&&e){
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
a=true; //设置a为true,以便是点击按钮时能再次切换fragment
e=false; //设置e为false,使在第一个fragment时能够退出
transaction.replace(R.id.show,firstFragment);
transaction.commit();
return false;
} else {
finish();
}
return super.onKeyDown(keyCode, event);
}

}

 

转载于:https://www.cnblogs.com/CCyz/p/6731166.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值