网站地图    收藏   

主页 > 前端 > css教程 >

Activity之startActivityForResult - html/css语言栏目:h

来源:自学PHP网    时间:2015-04-14 14:51 作者: 阅读:

[导读] main.xml如下:[html] RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android xmlns:tools=http://schemas.android.com/tools android:layout_width=fill_parent android:layout......

main.xml如下:

[html]  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    tools:context=".MainActivity" > 
 
    <TextView  
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerHorizontal="true" 
        android:text="@string/mainActivity_tip"  
        android:textSize="25sp" 
        /> 
    <Button 
        android:id="@+id/button" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerInParent="true" 
        android:text="@string/button_tip"  
        android:textSize="25sp"/> 
 
</RelativeLayout> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/mainActivity_tip"
        android:textSize="25sp"
        />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/button_tip"
        android:textSize="25sp"/>

</RelativeLayout>another.xml如下:

[html]  <?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout  
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
      
   <TextView  
       android:layout_width="200dip" 
       android:layout_height="50dip" 
       android:layout_centerHorizontal="true" 
       android:layout_marginTop="50dip" 
       android:gravity="center" 
       android:text="@string/anotherActivity_tip" 
       /> 
    
        
   <Button 
       android:id="@+id/closeButton" 
       android:layout_width="150dip" 
       android:layout_height="50dip" 
       android:layout_centerInParent="true" 
       android:layout_marginTop="50dip" 
       android:gravity="center" 
       android:text="@string/closeActivity" 
       /> 
</RelativeLayout> 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    
   <TextView
       android:layout_width="200dip"
       android:layout_height="50dip"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="50dip"
       android:gravity="center"
       android:text="@string/anotherActivity_tip"
       />
  
      
   <Button
       android:id="@+id/closeButton"
       android:layout_width="150dip"
       android:layout_height="50dip"
       android:layout_centerInParent="true"
       android:layout_marginTop="50dip"
       android:gravity="center"
       android:text="@string/closeActivity"
       />
</RelativeLayout>MainActivity如下:

[java]  package cn.com.bravesoft.testactivity3; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
/**
 * 利用startActivityForResult跳转到其余Activity
 * 且当第二个Activity关闭时将数据返回给第一个Activity
 */ 
public class MainActivity extends Activity { 
    private Button mButton; 
    private int requestCode=9527; 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        mButton = (Button) findViewById(R.id.button); 
        mButton.setOnClickListener(new ButtonOnClickListener()); 
    } 
 
    private class ButtonOnClickListener implements OnClickListener { 
        public void onClick(View v) { 
            Intent intent = new Intent(); 
            intent.setClass(getApplicationContext(), AnotherActivity.class); 
            startActivityForResult(intent, requestCode); 
        } 
    } 
     
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        super.onActivityResult(requestCode, resultCode, data); 
        System.out.println("调用onActivityResult方法 requestCode="+requestCode+",resultCode="+resultCode); 
        String name = data.getStringExtra("name"); 
        int age = data.getIntExtra("age", 0); 
        System.out.println("回传过来的参数:name="+name+",age="+age); 
    } 
 
     

package cn.com.bravesoft.testactivity3;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
 * 利用startActivityForResult跳转到其余Activity
 * 且当第二个Activity关闭时将数据返回给第一个Activity
 */
public class MainActivity extends Activity {
 private Button mButton;
 private int requestCode=9527;
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  mButton = (Button) findViewById(R.id.button);
  mButton.setOnClickListener(new ButtonOnClickListener());
 }

 private class ButtonOnClickListener implements OnClickListener {
  public void onClick(View v) {
   Intent intent = new Intent();
   intent.setClass(getApplicationContext(), AnotherActivity.class);
   startActivityForResult(intent, requestCode);
  }
 }
 
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  System.out.println("调用onActivityResult方法 requestCode="+requestCode+",resultCode="+resultCode);
  String name = data.getStringExtra("name");
  int age = data.getIntExtra("age", 0);
  System.out.println("回传过来的参数:name="+name+",age="+age);
 }

 
}
AnotherActivity如下:

[java] package cn.com.bravesoft.testactivity3; 
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; 
public class AnotherActivity extends Activity { 
    private Button mButton; 
    private int resultCode=23; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.another); 
        mButton=(Button) findViewById(R.id.closeButton); 
        mButton.setOnClickListener(new ButtonOnClickListenerImpl()); 
    } 
    private class ButtonOnClickListenerImpl implements OnClickListener{ 
        @Override 
        public void onClick(View v) { 
            Intent intent=new Intent(); 
            intent.putExtra("name", "小明"); 
            intent.putExtra("age", 12); 
            setResult(resultCode, intent); 
            finish(); 
        } 
    } 

package cn.com.bravesoft.testactivity3;
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;
public class AnotherActivity extends Activity {
 private Button mButton;
 private int resultCode=23;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.another);
        mButton=(Button) findViewById(R.id.closeButton);
        mButton.setOnClickListener(new ButtonOnClickListenerImpl());
 }
 private class ButtonOnClickListenerImpl implements OnClickListener{
  @Override
  public void onClick(View v) {
   Intent intent=new Intent();
   intent.putExtra("name", "小明");
   intent.putExtra("age", 12);
   setResult(resultCode, intent);
   finish();
  }
 }
}

 


 

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论