zoukankan      html  css  js  c++  java
  • Android学习之 Intent

    Intent 是Android Activity之间传递数据的桥梁!

    我们要从一个界面切换到另一个界面需要用到它,同时,应用间的相互调用也要用它!

    Android uses Intents to do specific jobs within applications.
    Android通过Intent在application之间完成指定的任务。

    下面,是一段我从《Android, A programmer’s guide》中摘的一段话,

    Once you master the use of Intents, a whole new world of application
    development will be open to you. This section defines what an Intent is and how it is used.
         An Intent is Android’s method for relaying certain information from one Activity to
    another. An Intent, in simpler terms, expresses to Android your intent to do something.
    You can think of an Intent as a message passed between Activities. For example, assume
    that you have an Activity that needs to open a web browser and display a page on your
    Android device. Your Activity would send an “intent to open x page in the web browser,”
    known as a WEB_SEARCH_ACTION Intent, to the Android Intent Resolver. The Intent
    Resolver parses through a list of Activities and chooses the one that would best match
    your Intent; in this case, the Web Browser Activity. The Intent Resolver then passes your
    page to the web browser and starts the Web Browser Activity.

    Intents 分为两大类:

    ●   Activity 间动作 Intents

        这些Intents用于在你的Application中呼叫其他的Activities.
        且只能一个Activity可以处理这个Intent, 例如,浏览一个网页,你需要打开一个Web Browser Activity.
    ●   广播型Intents

        这些Intents 发送给多个 Activities 进行处理.
        一个广播型Intent的例子是,当手机电量在低水平时,Android将会发出一个当前电量level的消息,任何
        Activity都可以进行处理,并执行相关的动作,例如消息某些Activity当电量低于某个特定点时!

    我学习的时候用了一个简单的例子:

    package nw.neugls.first;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class Android01 extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            TextView MyTextView=(TextView)findViewById(R.id.MyTextView);
            Button MyButton=(Button)findViewById(R.id.MyButton);
            
            MyTextView.setText("My first Activity");
            MyButton.setText("Click me");
            MyButton.setOnClickListener(new MyButtonListener());
                   
        }
        
        class MyButtonListener implements OnClickListener{
    
    		@Override
    		public void onClick(View arg0) {
    			// TODO Auto-generated method stub
    			Intent intent=new Intent();
    			intent.setClass(Android01.this,Activity02.class);
    			intent.putExtra("Key", "NeuglsWorkStudio");
    			Android01.this.startActivity(intent);
    		}
        	
        }
    }

    在上面的代码中,我们可以看到,我在这个Activity中包含两个控件:一个是TextView MyTextView, 另一个是Button MyButton.

    MyTextView用来显示一些标志信息,MyButton用于切换到另一个Activity中!

    findViewByID(int resourceID)只能被用在View 上面。这些View 是放进布局中的。而这个布局被Activity 使用setContentView()
    来加载的。

    类似于As3一样,需要给Android添加一OnClick监听!不过java的做法与As3不太一样!

    首先需要需要声明一个类实现onClickListener这个接口!在这类中我们需要覆盖onClick方法!我是初学者,对于怎么学我不知道,不过

    看了视频后,我学会了!有两种方法:

        方法1. 将鼠标放在MyButtonListener上,会出现一人提示:

           1

    点击Add unimplemented methods就可以了!

    方法2.点击右键或者按Alt+Shift+S,选择Override/Implements methods

         2

    然后我们往这个OnClick函数中写代码:

    		public void onClick(View arg0) {
    			// TODO Auto-generated method stub
    			Intent intent=new Intent();
    			intent.setClass(Android01.this,Activity02.class);
    			intent.putExtra("Key", "NeuglsWorkStudio");
    			Android01.this.startActivity(intent);
    		}
    

    Intent intent=new Intent() 创建了一个intent实例。

    intent.setClass(Android01.this,Activity02.class)

    下面是setClass的声明与参数:

    public IntentsetClass(Context packageContext, Class<?> cls)

    Parameters

    packageContext:  A Context of the application package implementing this class.
    实现这个类的应用程序包的Context。我猜测这个Context应该理解为实例!还请高手指证!

    cls: The class name to set, equivalent to setClassName(context, cls.getName()).
    要设置的类名!相当于setClassName(context, cls.getName()).

    意思是告诉Android, Context要启动一个Activity,他的名这是cls。

    intent.putExtra("Key", "NeuglsWorkStudio");

    这个很简单,往intent中加入一个Key-Value对!

    Android01.this.startActivity(intent);
    启动Activity.

    这就是第一个Activity的任务!

    下面是第二个Activity的程序:
    他从启动他的Intent获得数据,然后在一个TextView中显示出来。

    package nw.neugls.first;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class Activity02 extends Activity {
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		// TODO Auto-generated method stub
    		super.onCreate(savedInstanceState);
    		
    		setContentView(R.layout.activity02);
    		TextView tvActivity2=(TextView)findViewById(R.id.tvActivity02);
    		
    		Intent intent=getIntent();
    		
    		String str=intent.getStringExtra("Key");
    		
    		Button CreateMsgBtn=(Button)findViewById(R.id.btnCreateMsg);
    		CreateMsgBtn.setText("Create Message");
    		CreateMsgBtn.setOnClickListener(new CreateMsgBtnListener());
    		
    		tvActivity2.setText(str);
    	}
    	
    	class CreateMsgBtnListener implements OnClickListener{
    
    		@Override
    		public void onClick(View arg0) {
    			// TODO Auto-generated method stub
    			
    			Uri uri=Uri.parse("smsto://13539441914");
    			Intent intent=new Intent(Intent.ACTION_SENDTO,uri);
    			intent.putExtra("sms_body", "Write to neugls");
    			startActivity(intent);
    		}
    		
    	}
    
    }
    

    在这个Activity中还有一个Button CreateMsgBtn. 这个Button的OnClick代码中演示了Intent如何用于启动另一个App!上面代码是演示如何发送消息。

    下面是实现监听onClick事件的另一种写法:

    Button CreateMsgBtn=(Button)findViewById(R.id.btnCreateMsg);
    CreateMsgBtn.setText("Create Message");
    CreateMsgBtn.setOnClickListener(new OnClickListener(){
    	@Override
    	public void onClick(View v){
    		Intent intent=new Intent(Android01.this,Activity02.class);
    		//其他代码
    	}
    });
    ;
    
    
  • 相关阅读:
    footer点击添加active class
    css背景图与html插入img的区别
    js实现游戏转盘抽奖
    gulp压缩css和js
    前后端分离中,gulp实现头尾等公共页面的复用 前言
    js 输入框只能输入 1-7 的数字
    java 环境变量配置
    两日期相减得到天数
    jQuery如何追加tr到table中 添加到头或者尾
    json 添加 和删除两种方法
  • 原文地址:https://www.cnblogs.com/neugls/p/1969428.html
Copyright © 2011-2022 走看看