刚刚学习了Android不久,共粗略地学习了几个概念:Activity、Intent、Bundle,写了十分简单一个小程序,这个程序共有两个Activity类,用Intent实现它们间的跳转,用Bundle传递数据。
首先先介绍:Activity、Intent、Bundle
Activity的主要作用:
1. Activity是Android应用程序的一个用户接口,用户和应用程序直接进行交互的接口。
2. Activity实际上是一个各种控件的一个容器,我们把控件摆放进去。
Intent的作用:消息触发、消息传递、消息响应。
具体地说:Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将Intent传递给调用的组件,并完成组件的调用。因此,Intent在这里起着一个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解耦。
Bundle的作用:
Bundle类用作携带数据,它类似于Map,用于存放key-value名值对形式的值。
Toast的作用:
Toast用于向用户显示一些帮助/提示,别忘了调用show()方法。
当然,这三个类还有许多东西要学,特别是Activity,既然如此,我只能够后面再深入学咯。
在类包中添加两个类:HelloActivity和ReflectActivity。
HelloActivity代码:
public class HelloActivity extends Activity
{
private Button mainBtn=null;
private final static int REQUEST_CODE=1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainBtn=(Button)findViewById(R.id.button1);
mainBtn.setOnClickListener(listener);
}
private OnClickListener listener=new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent=new Intent();
intent.setClass(HelloActivity.this, ReflectActivity.class);
intent.putExtra("str", "你知道吗?");
startActivityForResult(intent, REQUEST_CODE);
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode==REQUEST_CODE)
{
if (resultCode==ReflectActivity.RESULT_CODE)
{
Bundle bundle=data.getExtras();
String str=bundle.getString("back");
Toast.makeText(HelloActivity.this, str, Toast.LENGTH_LONG).show();
}
}
}
}
ReflectActivity代码:
public classReflectActivity extends Activity
{
public final static int RESULT_CODE=1;
private TextView Txt;
private EditText editText;
private Button secondBtn;
@Override
protected void onCreate(BundlesavedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.reflect);
Intent intent=getIntent();
Bundlebundle=intent.getExtras();
Stringstr=bundle.getString("str");
Txt=(TextView)findViewById(R.id.text2);
Txt.setText(str);
secondBtn=(Button)findViewById(R.id.button2);
secondBtn.setOnClickListener(listener);
}
private OnClickListener listener=newOnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent=newIntent();
intent.putExtra("back","我知道…");
setResult(RESULT_CODE,intent);
finish();
}
};
}
在Layout中添加main.xml和reflect.xml(其实main.xml已经自动添加)
main.xml代码:(用于HelloActivity布局)
<?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:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello,what doyou want to talk to me?"
/>
<EditText
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape"
android:text="你知道吗??"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"
/>
</LinearLayout>
reflect.xml代码:(用于ReflectActivity布局)
<?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:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Ofcource"
/>
<EditText
android:id="@+id/reflect"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape"
android:text="我知道..."
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="reflect"
/>
</LinearLayout>
修改AndroidManiFest.xml
AndroidManiFest.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shen.android.learning"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".HelloActivity"
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
android:name=".ReflectActivity"
android:label="@string/app_name">
</activity>
</application>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
</manifest>
其实只需要添加以下几行:
<activity
android:name=".ReflectActivity"
android:label="@string/app_name">
</activity>