zoukankan      html  css  js  c++  java
  • android意图传參数(四)

    一、依照向导创建一个project,layout的activity_main.xml文件内容例如以下:

    <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=".MainActivity" >
    
        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="意向传參数測试"
          />
    
    </RelativeLayout>

    1)android:layout_width="match_parent" :谷歌把fill_parent改成了与实际效果更符合的match_parent,表示塞满容器,塞的意思就是有多少空间,占用多少空间

    2)android:layout_height="wrap_content" :设置为wrap_content将完整显示其内部的文本和图像。布局元素将依据内容更改大小。

    设置一个视图的尺寸为wrap_content大体等同于设置Windows控件的Autosize属性为True。

    3)android:text :设置button的显示值


    二、在Main函数做意向传參跳转

    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		button = (Button)this.findViewById(R.id.button);
    		button.setOnClickListener(new View.OnClickListener() {
    			
    			@Override
    			public void onClick(View arg0) {
    				// TODO Auto-generated method stub
    			   <span style="color:#ff0000;">  Intent intent = new Intent(MainActivity.this,OtherActivity.class);
    			     intent.putExtra("name", "Deng");
    			     intent.putExtra("age", 23);
    			     startActivity(intent);</span>
    			}
    		});
    	}
    


    通过startActivity方法启动


    三、新建一个other.xml 和 OtherActivity.class

    <?

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


    public class OtherActivity extends Activity {
    
    	private TextView textview;
    	public OtherActivity(){
    		
    	}
    	protected void onCreate(Bundle savedInstanceState){
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.other);
    		textview = (TextView)this.findViewById(R.id.msg);
    		<span style="color:#ff0000;">Intent intent = getIntent();</span>
    		<span style="color:#ff0000;">String name = intent.getStringExtra("name");
    		int age = intent.getIntExtra("age", 0);</span>
    		textview.setText("name"+name+";"+"age"+age);
    	}
    }

    记得重写onCreate方法以及设置setContentView布局


    四、最后在文件AndroidManifest.xml加入一个activity标签

     <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.android_intent.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>
        <span style="color:#ff0000;">   <activity
               android:name = ".OtherActivity" 
               /></span>
        </application>

            


  • 相关阅读:
    eclipse 下载 WindowBuilder
    CLOB、BLOB , CLOB与BLOB的区别
    rpm 安装并配置MySQL(包含指定数据存储路径)
    此Flash Player 与您的地区不相容,请重新安装Adobe Flash Player问题解决
    Eclipse 如何添加 更换字体(转载)
    Eclipse安装WebJavaEE插件、Eclipse编写HTML代码(综合问题统一记录)
    关于hp proliant sl210t服务器raid 1阵列配置(HP P420/Smart Array P420阵列卡配置)
    LINUX下EFIBOOTMGR的使用,删除UEFI主板多余启动项和添加启动项
    安装Linux系统时LSI RAID卡的驱动挂载
    IBM x3250m5安装redhat 6.5 加载raid卡驱动
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/6855542.html
Copyright © 2011-2022 走看看