从hello world开始

第一个android小程序, 学习性质, 实现2个activity之间的信息传递

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:parentActivityName="com.example.hello.MainActivity"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/hello_message"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:hint="@string/hello_message" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_send" 
        android:onClick="sendMessage"/>

</LinearLayout>

以上是第一个activity的layout, 包括 一个带hint的EditText以及一个用于发送的button, 整个activity采用线性布局, 对EditText分配权重1, 宽度0, 这样可以保证文本框撑屏幕一行.

//MianActivity.java
package com.example.hello;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {
	public final static String EXTRA_MESSAGE = "com.example.hello.MESSAGE";    

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@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;
	}
	
	 /** 当用户点击Send按钮时调用 */
	 public void sendMessage(View view) {
	    // 做一些相应按钮的操作
		 Intent Intent = new Intent(this,DisplayMessageActivity.class);
		 EditText TxtMsg = (EditText)this.findViewById(R.id.hello_message);
		 String message = TxtMsg.getText().toString();
		 Intent.putExtra(EXTRA_MESSAGE , message);
		 this.startActivity(Intent);
	 }

}

这是对用的java代码, 在绑定按钮点击事件的sendMessage中实现发送逻辑, 具体通过intend来实现, 可以看出intend需要绑定当前发送对象和接收类别(指定类的class).  这里多说一下, 因为接收信息对象是android自己创建的, 相当于托管, 所以我们无法绑定接收对象, 只能是绑定类别. 此外, intend是一个类似map结构的存在, 用键值对去存储传递信息. 设置好intend后就可以调用activity的方法启动另一个activity

<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=".DisplayMessageActivity" >

    <TextView
        android:id="@+id/send_msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

这是接收信息的activity, 包含一个TextView, 仅仅对接收到的信息简单显示

//DisplayMessageActivity.java
package com.example.hello;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_display_message);
		String msg = this.getIntent().getStringExtra(MainActivity.EXTRA_MESSAGE);
		TextView TxtSendMsg = (TextView)this.findViewById(R.id.send_msg);
		TxtSendMsg.setText(msg);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.display_message, menu);
		return true;
	}

}

这是对应的java程序, 在onCreate事件中实现了消息的接收.

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Hello</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="hello_tip">Hello everyone!</string>
    <string name="hello_message">enter a message</string>
    <string name="btn_send">Send</string>
    <string name="title_activity_display_message">DisplayMessageActivity</string>

</resources>
最后附上字符串资源文件


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值