这篇文章主要是将怎么新添加页面和怎么在两个页面之间跳转,以及怎么传值过去。
已经知道了页面配置文件在layout文件夹下,新建layout/report.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" > <TextView android:id="@+id/reportText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/second" /> </RelativeLayout>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dazlly" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".Main" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="Report" > </activity> </application> </manifest>这时候就可以调用了,改变Main.java的代码,让按钮可以点击触发事件:
package com.dazlly; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.app.Activity; import android.content.Intent; public class Main extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViews(); //声明控件对象 setListener(); //监听按钮点击方法 } private Button firstButton; private void findViews(){ firstButton=(Button)findViewById(R.id.firstButton); } private void setListener(){ firstButton.setOnClickListener(report); } //直接使用Button.OnClickListener是因为这样才是android.widget.Button private Button.OnClickListener report=new Button.OnClickListener(){ @Override public void onClick(View v) { Intent intent=new Intent(); intent.setClass(Main.this, Report.class); startActivity(intent); } }; }
package com.dazlly; import android.app.Activity; import android.os.Bundle; public class Report extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.report); } }
那么怎么才能把第一个页面的某些内容传值到第二个页面呢,下面我们实验一下,Main.java:
package com.dazlly; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.app.Activity; import android.content.Intent; public class Main extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViews(); // 声明控件对象 setListener(); // 监听按钮点击方法 } private Button firstButton; private TextView firstText; private void findViews() { firstButton = (Button) findViewById(R.id.firstButton); firstText = (TextView) findViewById(R.id.firstText); } private void setListener() { firstButton.setOnClickListener(report); } // 直接使用Button.OnClickListener是因为这样才是android.widget.Button private Button.OnClickListener report = new Button.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(Main.this, Report.class); Bundle bundle = new Bundle(); String str = firstText.getText().toString(); bundle.putString("TITLE", str); intent.putExtras(bundle); startActivity(intent); } }; }
package com.dazlly; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class Report extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.report); findViews(); getBundle(); } private TextView reportText; private void findViews() { reportText = (TextView) findViewById(R.id.reportText); } private void getBundle() { Bundle bundle = this.getIntent().getExtras(); String title = bundle.getString("TITLE"); reportText.setText(title); } }
另外还有从跳转的页面带着数据再回来,这个要用到startActivityForResult(intent,0)方法以及重写onActivityResult方法,这里简单说下相关代码,有兴趣的可以自己尝试一下,Main.java部分:
startActivityForResult(intent, 0); } }; @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (resultCode) { case RESULT_OK: Bundle bundle = data.getExtras(); String title = bundle.getString("TITLE"); firstText.setText("back " + title); break; default: break; } }
reportButton.setOnClickListener(back); } private Button.OnClickListener back = new Button.OnClickListener() { @Override public void onClick(View v) { Report.this.setResult(RESULT_OK, Report.this.getIntent()); Report.this.finish(); } };