首页 > 代码库 > Activity

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);
}

}

 

Activity