您的位置:首页 > 移动开发 > Android开发

Android基础总结1 ----页面传值和事件监听

2014-10-08 16:37 351 查看
Android基础总结1 ----页面传值和事件监听

A 页面传值

Android中在两个Activity中相互传送数值,主要有三种方法:

方法一: 如果数据较少,比如只要传一个字符串

可以使用如下代码:只用Intent组件完成(在Android中Intent是不同组件相互通信的纽带)。

在Activity A中:

Intent intent = new Intent(A.this, B.class);
intent.putExtra("Name", "Value");
startActivity(intent);
在Activity B中:

Intent intent = this.getIntent();
String name = intent.getStringExtra("Name");
上面的例子中是由Activity A(发) 对 Activity B(收)传值,其中putExtra中的参数组成了键值对key-value的操作,第一个参数是键名,第二的是值,就是要传送的值。在Activity B中通过获取键名(Name)就可以获取要传送的值了。

方法二:如果数据比较多,就需要使用 Bundle类

代码如下:要配合使用Bundle类,Bundle类也是一个key-value对。

在Activity A中传值:

Intent intent = new Intent(A.this, B.class);
//通过Bundle对象存储需要传递的数据
Bundle bundle = new Bundle();
//字符、字符串、布尔、字节数组、浮点数等等,都可以传
bundle.putString("Name", "Value");
bundle.putBoolean("Isgood", true);
//把bundle对象assign给Intent
intent.putExtras(bundle)
startActivity(intent);


在Activity B中获取传递的值:

//获取Intent中的Bundle对象
Bundle bundle = this.getIntent().getExtras();
//获取Bundle中的数据,注意类型和key
String name = bundle.getString("Name");
boolean ismale = bundle.getBoolean("Isgood");


方法三:如果想回传数据,就需要用startActivityForResult()方法了

上面的都是用的是startActivity(intent)方法。那二者的区别是什么?
1、startActivity( ) 仅仅是跳转到目标页面,若是想跳回当前页面,则必须再使用一次startActivity( )。

2、startActivityForResult( ) 可以一次性完成这项任务. 当程序执行到这段代码的时候,假若从T1Activity跳转到下一个T2Activity,而当这个T2Activity调用了finish()方法以后,程序会自动跳转回T1Activity,并调用前一个T1Activity中的onActivityResult( )方法。

相关函数用

startActivityForResult(Intent intent, Int requestCode)
setResut(int resultCode, Intent intent)
onActivityResult(int requestCode, int resultCode, Intent intent)


B 事件监听

以按钮的监听为例,可以通过3种方式实现按钮的监听事件:1.通过匿名内部类的方式;2.通过独立类的方式;3.通过实现onClickListener接口的方式。

总体的程序如下:

这个是MainActivity

MainActivity


package com.example.activitydome1;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

private Button myButton1;
private TextView tvOut;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tvOut = (TextView) findViewById(R.id.tvOut);

myButton1 = (Button) findViewById(R.id.button1);
/*
* 可以通过3种方式实现按钮的监听事件:1.通过匿名内部类的方式;2.通过独立类的方式;3.通过实现onClickListener接口的方式
* 匿名内部类的监听事件
*/
myButton1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
//启动一个新的Activity
Intent i = new Intent(MainActivity.this, MyActivityDome1.class);

//i.putExtra("text1", "hello myATY1");

//使用Bundle(key--value)  两个Activity之间传送数据
Bundle myBundleData = new Bundle();
myBundleData.putString("text2","hello myQYLzc2");

i.putExtras(myBundleData);

/*
* 1、startActivity( ) 仅仅是跳转到目标页面,若是想跳回当前页面,则必须再使用一次startActivity( )。
* 2、startActivityForResult( ) 可以一次性完成这项任务.
* 当程序执行到这段代码的时候,假若从T1Activity跳转到下一个T2Activity,而当这个T2Activity调用了finish()方法以后,
* 程序会自动跳转回T1Activity,并调用前一个T1Activity中的onActivityResult( )方法。
* 相关函数:startActivityForResult(Intent intent, Int requestCode)
* setResut(int resultCode, Intent intent)
* onActivityResult(int requestCode, int resultCode, Intent intent)
*/

//跳转的时候不是采用startActivity(intent) 这个方法,而是startActivityForResult(intent, 0)
//startActivity(i);
startActivityForResult(i,0);//这里采用startActivityForResult来做跳转,此处的0为一个依据,可以写其他的值,但一定要>=0
}
});
System.out.println("onCreate method");
Log.i("tag", "onCreate method");
}

/*
* 重写onActivityResult方法,用来接收MyActivityDome1回传的数据。
* 在MyActivityDome1中回传数据时采用setResult方法,并且之后要调用finish方法。
*/
@Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
super.onActivityResult(arg0, arg1, arg2);

String result = arg2.getStringExtra("result");//获得回传的数据
tvOut.setText(result);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
protected void onStart() {

super.onStart();
System.out.println("onStart method");
}

@Override
protected void onStop() {

super.onStop();
System.out.println("onStop method");
}

@Override
protected void onResume() {

super.onResume();
System.out.println("onResume method");
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
System.out.println("onPuase method");
}

@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
System.out.println("onRestart method");
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("onDestroy method");

}
}
</pre><p></p><p><span style="font-size:18px"></span></p><pre name="code" class="java">这个是子页面的文件,MyActivityDome1.java
</pre><pre name="code" class="java">package com.example.activitydome1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class MyActivityDome1 extends Activity implements OnClickListener {

private Button myButton2;
private TextView tvOut;
private ImageButton iButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myacy1);

myButton2 = (Button) findViewById(R.id.btnClose);

/*
* 可以通过3种方式实现按钮的监听事件:1.通过匿名内部类的方式;2.通过独立类的方式;3.通过实现onClickListener接口的方式
* 匿名内部类的监听事件
*/
myButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent i = new Intent();//定义回传数据的Intent,当然也可以自己定义新的Bundle
i.putExtra("resurt", "Hollow MainActivity!");
/*
* 在MyActivityDome1中回传数据时采用setResult方法,并且之后要调用finish方法。
* finish()方法调用完成后直接,直接回到MainActivity中的onActivityResult()方法中;
*
* 注意:知识点
* Android activity的setResult()在什么时候调用?(重点也是难点)
* 如果在startActivityForResult起来的Activity里面设置setResult,结果并不会马上返回给parent的Activity,
* 只有当前Activity被finish,结果才会被发送给parent的onActivityResult去处理!
*
* 也就是说调用setResult()方法必须在finish()之前。
* 那么如果在如下方法中调用setResult()也有可能不会返回成功:
* onPause(), onStop(), onDestroy(),因为这些方法调用不一定是在finish之
* 前的,当然在onCreate()就调用setResult肯定是在finish之前的
*/
setResult(0, i);
finish();
}
});

iButton = (ImageButton) findViewById(R.id.imageButton1);
/*
* 可以通过3种方式实现按钮的监听事件:1.通过匿名内部类的方式;2.通过独立类的方式;3.通过实现onClickListener接口的方式
* 通过实现onClickListener接口的方式
*/
iButton.setOnClickListener(this);

tvOut = (TextView) findViewById(R.id.tvOut);
//tvOut.setText(getIntent().getStringExtra("text1"));
Bundle data = getIntent().getExtras();
String myString = data.getString("text2");

tvOut.setText(myString);
}
@Override
public void onClick(View v) {
Log.i("tag", "通过实现onClickListener接口的方式");

}

}


这个是主页面的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="com.example.activitydome1.MainActivity" >

<TextView
android:id="@+id/tvOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:text="@string/hello_world" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvOut"
android:layout_marginTop="24dp"
android:text="Start_myacy1" />
</RelativeLayout>
这个是子页面的XMl文件

<?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/tvOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28sp"
android:textColor="#ffee00"
android:text="myacy1" />

<Button
android:id="@+id/btnClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关闭" />

<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f0f0f0"
android:src="@drawable/ic_launcher" />

<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陆" />

<ImageButton
android:id="@+id/imageButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFF000"
android:src="@drawable/ic_launcher" />

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: