调用另一个Activity

 

参考自Google官方文档Traning/Getting Started/Building a simple user interface, Startinganother activity,http://developer.android.com/training/basics/firstapp/building-ui.html

1、创建主Activity

使用Eclipse新建项目MyFirstApp,UI布局如下:

<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:orientation="horizontal"
    tools:context=".MainActivity">
 
    <EditText
        android:id="@+id/et_message"
        android:layout_height="wrap_content"
       android:layout_width="0dp"
        android:layout_weight="1"
        android:hint="@string/input_here"/>
   
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/click"
        android:onClick="sendMessage"/>
 
</LinearLayout>


注意通过权重来分配尺寸的方式

组件1:

android:layout_width="0dp"
android:layout_weight="1"

组件2:

android:layout_width="wrap_content"

2、在主类中指定onclick所对应的sendMessage方法

package com.lujinhong.androidtraningmyfirstapp;
 
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.lujinhong.myfirstapp.MESSAGE";
 
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        // Inflate themenu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
   
    public void sendMessage(View v){
                   
        EditText et_message=(EditText)this.findViewById(R.id.et_message);
        String message= et_message.getText().toString().trim();
       
        Intent intent= new Intent(this,DisplayMessageActivity.class);
        intent.putExtra(EXTRA_MESSAGE, message);
       
        this.startActivity(intent);
       
    }
 
}


(1)关于intent

An Intent isan object that provides runtime binding between separate components (such astwo activities). TheIntent representsan app’s "intent to do something." You can use intents for a widevariety of tasks, but most often they’re used to startanother activity.

(2)调用另一个activity的步骤:

l  首先取得editText中的文字

EditText et_message = (EditText) this.findViewById(R.id.et_message);
String message = et_message.getText().toString().trim();

l  然后创建一下intent,并把文字作为K-V形式保存到intent中

        Intent intent= new Intent(this,DisplayMessageActivity.class);
        intent.putExtra(EXTRA_MESSAGE, message);

创建intent时,通过一个类名,指定调用哪个类文件。

l  最后启动一个新的activity.

this.startActivity(intent);

3、显示另一个Activity

package com.lujinhong.androidtraningmyfirstapp;
 
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;
 
public class DisplayMessageActivityextends Activity{
 
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);
       
        // Get the messagefrom the intent
        Intent intent= getIntent();
        String message= intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
 
        // Create the textview
        TextView textView=new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);
 
        // Set the textview as the activity layout
        setContentView(textView);
       
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        // Inflate themenu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.display_message, menu);
        return true;
    }
 
}


 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值