android基础之页面间跳转传值,用Bundle+Intent

来源:互联网 发布:广州淘宝进货都在哪里 编辑:程序博客网 时间:2024/06/14 01:51

这是很基础的android知识了,适合刚刚接触android的朋友,下面用一个例子说明下android是怎么进行页面间跳转传值的,废话不多说了。
这是整个项目,包含一个4个页面,其中main_activity.xml为主页面,严格来说,android里面是一个页面(.xml)对应一个Activity,当然不排除Fragment,这个我们后面再讲吧!
照着建就行
好!现在咱们有了页面和Activity,只差写代码了,代码很简单的
main_activity.xml:

<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=".MainActivity" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="请输入要传递的值:" />    <EditText        android:id="@+id/et"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:hint="输入要传递的值" />    <Button        android:id="@+id/btn1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:hint="跳到页面A" />    <Button        android:id="@+id/btn2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:hint="跳到页面B" />    <Button        android:id="@+id/btn3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:hint="跳到页面C" /></LinearLayout>

activity_a.xml:

<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=".MainActivity" >    <TextView        android:id="@+id/tv1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="" />    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="页面A" /></LinearLayout>

activity_b.xml:

<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=".MainActivity" >    <TextView        android:id="@+id/tv2"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="" />    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="页面B" /></LinearLayout>

activity_c.xml:

<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=".MainActivity" >    <TextView        android:id="@+id/tv3"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="" />    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="页面C" /></LinearLayout>

MainActivity.java里面

package cn.android.value;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity implements OnClickListener{    private EditText et;    private TextView tv;    private Button btn1,btn2,btn3;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        et = (EditText)findViewById(R.id.et);        btn1 = (Button) findViewById(R.id.btn1);        btn2 = (Button) findViewById(R.id.btn2);        btn3 = (Button) findViewById(R.id.btn3);        btn1.setOnClickListener(this);        btn2.setOnClickListener(this);        btn3.setOnClickListener(this);    }    @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 void onClick(View v) {        String a = et.getText().toString();        switch (v.getId()) {        case R.id.btn1:            Intent intent1 = new Intent();            Bundle bundle=new Bundle();            intent1.setClass(this, ActivityA.class);            bundle.putString("Name", a);            intent1.putExtras(bundle);            startActivity(intent1);            break;        case R.id.btn2:            Intent intent2 = new Intent();            Bundle bundle2=new Bundle();            intent2.setClass(this, ActivityB.class);            bundle2.putString("Name", a);            intent2.putExtras(bundle2);            startActivity(intent2);            break;        case R.id.btn3:            Intent intent3 = new Intent();            Bundle bundle3=new Bundle();            intent3.setClass(this, ActivityC.class);            bundle3.putString("Name", a);            intent3.putExtras(bundle3);            startActivity(intent3);            break;        default:            break;        }    }}

ActivityA.java里面:

package cn.android.value;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageView;import android.widget.TextView;public class ActivityA extends Activity implements OnClickListener{    private TextView textView;    @Override    public void onClick(View arg0) {        // TODO Auto-generated method stub    }    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_a);        textView=(TextView) findViewById(R.id.tv1);        Bundle bundle = this.getIntent().getExtras();        String name = bundle.getString("Name");        textView.setText(name+"");    }}

ActivityB.java里面:

package cn.android.value;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.TextView;public class ActivityB extends Activity implements OnClickListener{    private TextView textView;    @Override    public void onClick(View arg0) {        // TODO Auto-generated method stub    }    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_b);        textView=(TextView) findViewById(R.id.tv2);        Bundle bundle = this.getIntent().getExtras();        String name = bundle.getString("Name");        textView.setText(name+"");    }}

ActivityC.java里面

package cn.android.value;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.TextView;public class ActivityC extends Activity implements OnClickListener{    private TextView textView;    @Override    public void onClick(View arg0) {        // TODO Auto-generated method stub    }    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_c);        textView=(TextView) findViewById(R.id.tv3);        Bundle bundle = this.getIntent().getExtras();        String name = bundle.getString("Name");        textView.setText(name+"");    }}

好,代码咱们就写完了,运行下,发现报错了,什么原因呢?我们忘记了很重要的一步,注册Activity,打开项目文件AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="cn.android.value"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="18" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <activity            android:name="cn.android.value.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity            android:name="cn.android.value.ActivityA"            android:screenOrientation="portrait" />        <activity            android:name="cn.android.value.ActivityB"            android:screenOrientation="portrait" />        <activity            android:name="cn.android.value.ActivityC"            android:screenOrientation="portrait" />    </application></manifest>

这里要记住的是name=”这里面的路径要正确”

好了,现在运行就没错了,看看效果吧
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这个demo很简单明了的说明了页面 的跳转和传值,后续我会陆续更新别的内容,也欢迎大家加入咱们的安卓技术交流群,
群号: 2308505

0 0
原创粉丝点击