这是很基础的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

更多相关文章

  1. Android里Context的使用
  2. Android数据库操作的简单封装
  3. Android(安卓)Windows 开发环境搭建教程
  4. COCOS2D-X跨ANDROID&IOS平台开发入门教程
  5. Android:HttpClient工具类
  6. Android(安卓)基于google Zxing实现二维码、条形码扫描,仿微信二
  7. Android(安卓)通过URL scheme 启动App
  8. android软键盘把页面顶上去
  9. 简单处理Android(安卓)65536方法越界问题

随机推荐

  1. 【Android(安卓)开发教程】保存到外部存
  2. 用kotlin写了一些android常用的一些工具
  3. Android(安卓)Gson深入分析
  4. Windows 环境下载 Android(安卓)源码
  5. Gradle???????
  6. Android扫描二维码及生成二维码Demo
  7. 窗口关系
  8. Android(安卓)文本转语音TextToSpeech (T
  9. Android(安卓)ApiDemos示例解析(177):Vie
  10. Android(安卓)SipDemo项目实现SIP协议