Android页面交互Activity组件(上)

来源:互联网 发布:内存卡误删恢复软件 编辑:程序博客网 时间:2024/05/20 04:31

Activity组件:负责用户交互组件(.java页面),Intent在Activity组件中应用于不同组件

               (页面)之间通信的载体,Intent可以启动应用程序中另一个Activity

                        startActivity( Intent  intent)方法,该方法中的 Intent参数封装了启动的目标Activity

1新建project,

2.在res/layout目录下新建xml,这里命名main2.xml

3在src/com.main目录下新建class,这里命名为FourActivity和SecondActivity

main.xml源码:

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


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <EditText 
        android:layout_height="wrap_content"
        android:layout_width="fill_parent" 
        android:id="@+id/high"/>
    <Button
         android:layout_height="wrap_content"
         android:layout_width="fill_parent"
         android:text="按钮" 
         android:id="@+id/button1"
         android:textSize="20dp"/>


</LinearLayout>

布局:

main2.xml源码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:text="体重"
        android:textSize="20dp"
        android:id="@+id/a"/>
    <Button
         android:layout_height="wrap_content"
         android:layout_width="fill_parent"
         android:text="跳转" 
         android:id="@+id/count"
         android:textSize="20dp"/>
    
    
   </LinearLayout>





FourActivity.java文件一源码:

package com.main;      
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.EditText;

public class FourActivity extends Activity {
    /
** Called when the activity is first created. */
private  Button button1;

private  EditText editText1;
         OnClickListener l=new  OnClickListener( ){   
 
        @Override
        public void onClick (View v) {
        
        String Str=editText1.getText().toString(); //获取文本框文本
        Intent intent =new  Intent(FourActivity.this,SecondActivity.class);
                          //从FourActivity页面转到SecondActivity页面
        
                        Bundle data=new Bundle();    //创建了一个Bundle对象data
        data.putString("str", Str);  //将Str对象封装在data里,str为拆箱字符
        intent.putExtras(data);      //将封装好的数据寄存到intent(intent为信使)
        
                       
        
         }
     };
     
     
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);  
         button1 = (Button) findViewById (R.id.button1);//获取指定id的界面控件
         editText1=(EditText)  findViewById (R.id.high);
 }
}


SecondActivity.java 

文件二源码:

package com.main;


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 SecondActivity extends Activity{
private  Button button2;

private  TextView edittext2;
//final OnClickListener l=new  OnClickListener( ){

  @Override
      public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main2);
         
         Intent intent= getIntent();//获取启动
SecondActivity的Intent

         Bundle b=intent.getExtras();//获取Intent所携带的数据

         String str1=b.getString("str");从
Bundle包中去key为str的数据

         edittext2=(TextView) findViewById(R.id.a);
         
         edittext2.setText(str1);   

        //将Intent携带拆箱后的数据填充现文本
         
         button2=(Button)  findViewById(R.id.count);
         button2.setOnClickListener(new OnClickListener() {
 //按钮
button2绑定事件监听器,点击按钮button2事件结束


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();事件结束

}
});
 
  }
  }


FourActivity

完成将页面一的输入文本框封装并传入页面二;


SecondActivity

接受页面二的文本框内容改为文本视图;



此外还需设置AndroidManifest.xml


原创粉丝点击