• android学习之旅——01——Activity与Intent


    Intent的基本作用

    一个Intent对象包含了一组信息

    1,Component name

    2,Action

    3,Data

    4,Category

    5,Extras

    6,Flags

     第一个 Activity: Android02Activity

    package com.org;
    
    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;
    
    public class Android02Activity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //布局
            setContentView(R.layout.main);
            //取得Button对象
            Button btn = (Button)findViewById(R.id.mybtn);
            btn.setText("点击");
            //btn注册事件
            btn.setOnClickListener(new MyBtnListener());
        }
        class MyBtnListener implements OnClickListener{
        	@Override
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		//生成一个intent对象
        		Intent intent = new Intent();
        		//intent传递数据
        		intent.putExtra("name", "jingjing"); 
        		intent.setClass(Android02Activity.this, OtherActivity.class);
        		//启动intent
        		Android02Activity.this.startActivity(intent);
        		
        		//发送短信
        		/*Uri uri = Uri.parse("smsto://0800000123");
        		Intent intent = new Intent(Intent.ACTION_SENDTO,uri);
        		intent.putExtra("name", "lisi");
        		startActivity(intent);*/
        	}
        }
    }
    
    第二个Activity: OtherActivity
    
    package com.org;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class OtherActivity extends Activity {
    	private TextView text ;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		// TODO Auto-generated method stub
    		super.onCreate(savedInstanceState);
    		// 如果是R.layout.main 提示错误
    		setContentView(R.layout.other);
    		
    		Intent intent = getIntent();
    		String name = intent.getStringExtra("name");
    		
    		text = (TextView)findViewById(R.id.mytext);
    		text.setText(name);
    	}
    }
    


     

    Android02Activity的布局文件 main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <Button
        android:id="@+id/mybtn"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        />
    </LinearLayout>
    


    OtherActivity的布局文件 other.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView
        android:id="@+id/mytext"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        />
    </LinearLayout>


    最后要在AndroidManifest.xml文件中注册 Activity

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.org"
          android:versionCode="1"
          android:versionName="1.0">
        <uses-sdk android:minSdkVersion="4" />
    
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".Android02Activity"
                      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 -->
            <activity android:name=".OtherActivity"
                      android:label="@string/other">
         
            </activity>
    
        </application>
    </manifest>


    不然会报异常

  • 相关阅读:
    man LVCREATE
    Pyhton---基础---递归
    Python---基础---循环,函数
    Python---基础---dict_tuple_set
    Python---基础---dict和set2
    安卓网络编程学习(1)——java原生网络编程(1)
    使用python爬取B站弹幕和三连
    maven项目整合工具学习
    读阿里巴巴泰山版《java开发手册》总结(2)
    第十三周学习进度
  • 原文地址:https://www.cnblogs.com/itmyhome/p/4131611.html
Copyright © 2020-2023  润新知