本章使用Intent,回传需要的结果;
流程:
启动主页->按下按键启动第二个activity->输入用户名,按下OK按键->回传用户名回主页->主页读取用户名并显示出来。
1 启动主页图:
2 启动第二个activity
3 输入用户名:
4 回传到主页,读取并显示:
新建项目,然后输入对应的代码就OK了。
1 主页界面代码(activity_main.xml):
<LinearLayout 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"
tools:context="com.example.intenttest.MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/activate_secondary"
android:onClick="onClick" />
</LinearLayout>
建立一个Button,然后指定相应时间是onClick
2 主页的响应时间代码:
public class MainActivity extends Activity {
/*
* An integer value that identifies an activity you are calling.
* When an Activity returns a value, you must have a way to identify it
* If the request code is set to -1, then calling it using the
* startActivityForResult() method is equivalent to calling it using
* the startActivity() method. That is, no result will be returned.
*/
int requestCode = 1;
public static<T> boolean equ(T t1, T t2){
return t1 == t2;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view){
startActivityForResult(new Intent("su.bill.intenttest.SecondaryActivity"), requestCode);
}
}
这里和一般的startActivity不用的就是使用了startActivityForResult,使用这个函数就能回传结果了。
3 进入第二个activity的界面代码:
<?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="horizontal" >
<EditText
android:id="@+id/txt_username"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/your_name" />
<Button
android:id="@+id/btn_OK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="@string/ok_button" />
</LinearLayout>
简单的一个输入文本,一个按键
4 第二个activity的逻辑代码
package com.example.intenttest;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class SecondaryActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.secondary_activity);
}
public void onClick(View v){
Intent data = new Intent();
EditText txt_username = (EditText) findViewById(R.id.txt_username);
data.setData(Uri.parse(txt_username.getText().toString()));
setResult(RESULT_OK, data);
finish();
}
}
主要是onClick代码,新建一个Intent data之后,设置好这个Intent的内容,然后利用setResult回传这个Intent给主页activity,其实基本原理还是利用Intent通信。
5 主页逻辑的读取并显示结果的代码:
@Override
public void onActivityResult(int reqCode, int resCode, Intent data) {
if (equ(reqCode, requestCode) && equ(resCode, RESULT_OK)){
Toast.makeText(this, data.getDataString(), Toast.LENGTH_SHORT).show();
}
}
从上一个对应的activity返回的时候就自动调用处理的函数,其中的表示符号就是requestCode和返回的标识符reqCode对应起来,以便确认这个返回的Intent是对应的activity返回的。
主页逻辑的完整代码如下:
package com.example.intenttest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
/*
* An integer value that identifies an activity you are calling.
* When an Activity returns a value, you must have a way to identify it
* If the request code is set to -1, then calling it using the
* startActivityForResult() method is equivalent to calling it using
* the startActivity() method. That is, no result will be returned.
*/
int requestCode = 1;
public static<T> boolean equ(T t1, T t2){
return t1 == t2;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view){
startActivityForResult(new Intent("su.bill.intenttest.SecondaryActivity"), requestCode);
}
@Override
public void onActivityResult(int reqCode, int resCode, Intent data) {
if (equ(reqCode, requestCode) && equ(resCode, RESULT_OK)){
Toast.makeText(this, data.getDataString(), Toast.LENGTH_SHORT).show();
}
}
@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);
}
}
其他没说到的代码都是新建项目的时候自动生成的代码了,可以暂时忽略。
就是熟悉好Android的一些机制,理解起来倒是没有什么难度的。
说好是百日程序,看来不能百日之内完成了,因为还有很多其他事情做。
争取做快点吧,以后出点精品教程,把学到的,让更多人学会也是很有意义的事情的。