今天翻阅了以前写的一些代码,感觉还是有必要贴出来分享一下,这是一个以Intent为主体的分页代码,很简单。
就不做详细解释了,
首先要先新建一个SecondActivity.java和一个second.xml。然后在MainActivity中加入如下代码:
package com.example.intent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button mainBtn=null;
private final static int REQUEST_CODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainBtn=(Button)findViewById(R.id.mainbtn);
mainBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent=new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
intent.putExtra("str", "Intent Demo");
startActivityForResult(intent, REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==REQUEST_CODE){
if(resultCode==SecondActivity.RESULT_CODE){
Bundle bundle=data.getExtras();
String string=bundle.getString("back");
Toast.makeText(MainActivity.this, string, Toast.LENGTH_LONG).show();
}
}
}
@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);
}
}
在SecondActivity加入如下代码:
package com.example.intent;
import android.R.integer;
import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class SecondActivity extends Activity {
private TextView secondTxt;
private Button button;
public final static int RESULT_CODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
String string=bundle.getString("str");
secondTxt=(TextView)findViewById(R.id.secondTxt);
secondTxt.setText(string);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent=new Intent();
intent.putExtra("back", "back date");
setResult(RESULT_CODE, intent);
finish();
}
});
}
}
然后在main.xml加入以下代码:
<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.intent.MainActivity" >
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="点击按钮"
/>
<Button
android:id="@+id/mainbtn"
android:layout_below="@id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="button"
/>
</RelativeLayout>
在second.xml加入以下代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/secondTxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="点击按钮"
/>
<Button
android:id="@+id/button"
android:layout_below="@id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Second"
/>
</LinearLayout>
重要的是在AndroidMainifest.xml加入一个Activity:
<activity
android:name=".SecondActivity"
android:label="@string/app_name" >
</activity>
否则的话,程序就运行不了,在以后的程序中,只要涉及多个页面,都要声明相应的类。