換頁的第2頁(SecondActivity)建立與動態/動態/MainActivity
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
PlaceholderFragment
placeholderFragment;
@Override
protected void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
placeholderFragment=new
PlaceholderFragment();
if
(savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container,
placeholderFragment).commit();
}
Bundle
dataBundle=new Bundle();
dataBundle.putString("str2",
"this is");
placeholderFragment.setArguments(dataBundle);
}
@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.thirdBtn){
Intent
i=new Intent(this,ThirdActivity.class);
startActivity(i);
}
return true;
}
/**
* A placeholder fragment containing a simple
view.
*/
public static class
PlaceholderFragment extends Fragment {
EditText nameField;
EditText ageField;
EditText pawField;
public
PlaceholderFragment() {
}
@Override
public View
onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle
savedInstanceState) {
View
rootView = inflater.inflate(R.layout.fragment_main, container,
false);
nameField=(EditText)rootView.findViewById(R.id.nameField);
ageField=(EditText)rootView.findViewById(R.id.ageField);
pawField=(EditText)rootView.findViewById(R.id.pawField);
Button
secondBtn=(Button)rootView.findViewById(R.id.secondBtn);
secondBtn.setOnClickListener(new
View.OnClickListener() {
@Override
public
void onClick(View v) {
String
name=nameField.getText().toString();
String
age=ageField.getText().toString();
String
password=pawField.getText().toString();
Intent
i=new Intent(PlaceholderFragment.this.getActivity(),SecondActivity.class);
i.putExtra("name",
name).putExtra("age", age).putExtra("paw", password);
startActivity(i);
}
});
return
rootView;
}
}
}
換頁的第2頁(SecondActivity)建立與動態/建立/Layout
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.sample14.SecondActivity$PlaceholderFragment"
>
<TextView
android:id="@+id/nameLabel1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_marginRight="10dp"
android:text="Name:"
/>
<TextView
android:id="@+id/ageLabel1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/nameLabel1"
android:layout_below="@id/nameLabel1"
android:text="Age:"
android:layout_marginTop="20dp"/>
<TextView
android:id="@+id/pawLabel1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/nameLabel1"
android:layout_below="@id/ageLabel1"
android:text="password:"
android:layout_marginTop="20dp"/>
<EditText
android:id="@+id/nameField1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/nameLabel1"
android:editable="false"
android:inputType="none"
android:layout_alignBaseline="@id/nameLabel1"/>
<EditText
android:id="@+id/ageField1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/nameField1"
android:layout_alignRight="@id/nameField1"
android:layout_alignBaseline="@id/ageLabel1"
android:editable="false"
android:inputType="none"
/>
<EditText
android:id="@+id/pawField1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/pawLabel1"
android:layout_alignRight="@id/nameField1"
android:layout_alignBaseline="@id/pawLabel1"
android:inputType="none"
android:editable="false"
android:layout_marginLeft="10dp"
/>
</RelativeLayout>
換頁的第2頁(SecondActivity)建立與動態/動態/SecondActivity
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class SecondActivity extends Activity {
@Override
protected void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent i=getIntent();
/*
*/
PlaceholderFragment
placeholderFragment=new PlaceholderFragment();
placeholderFragment.myIntent=i;
if
(savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container,
placeholderFragment).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.second,
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);
}
/**
* A placeholder fragment containing a simple
view.
*/
public static class
PlaceholderFragment extends Fragment {
public Intent
myIntent;
public
PlaceholderFragment() {
}
@Override
public View
onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle
savedInstanceState) {
View
rootView = inflater.inflate(R.layout.fragment_second,
container,
false);
String
name=myIntent.getStringExtra("name");
String
age=myIntent.getStringExtra("age");
String
paw=myIntent.getStringExtra("paw");
return
rootView;
}
}
}
沒有留言:
張貼留言