开始学习android,相信自己,成就未来!下面是我的练习演示,希望大家多多指教。
程序运行展示:
一、名为FirstAction的Activity界面:
布局文件main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <EditText android:text="" android:id="@+id/EditText01" android:layout_width="fill_parent" android:layout_height="wrap_content"></EditText> <EditText android:text="" android:id="@+id/EditText02" android:layout_width="fill_parent" android:layout_height="wrap_content"></EditText> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:text="submit" android:layout_height="wrap_content" android:id="@+id/Button01"></Button> </LinearLayout> </LinearLayout>
二、当输入文本为空时,弹出提示框。
三、输入为空的文本框背景色变红
四、完成输入,并点击submit按钮
五、跳到名为SecondAction的Activity,并把前一个Activity输入的文本显示出来。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <Button android:text="返回上一个activity" android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout>
六、从SecondAction返回的数据并显示出来,点击确定结束该练习程序。
下面贴出两个Activity的源码:
第一个Activity——FirstAction.java
package com.study.activitytest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class FirstAction extends Activity {
/** 第一个输入文本框 */
private EditText editText1;
/** 第二个输入文本框 */
private EditText editText2;
/** 提交到SecondAction的按钮 */
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText1 = (EditText) findViewById(R.id.EditText01);
editText2 = (EditText) findViewById(R.id.EditText02);
button = (Button) findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String ipt1 = editText1.getText().toString();
//简单验证输入框
if ("".equals(ipt1.trim())) {
alert("输入不能为空", "提示",new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
editText1.setText("");
editText1.setBackgroundColor(Color.RED);
editText1.requestFocus();
}
});
return;
}
String ipt2 = editText2.getText().toString();
if ("".equals(ipt2.trim())) {
alert("输入不能为空", "提示",new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
editText2.setText("");
editText2.setBackgroundColor(Color.RED);
editText2.requestFocus();
}
});
return;
}
//用Bundle存储数据
Bundle bundle = new Bundle();
bundle.putString("ipt1", ipt1);
bundle.putString("ipt2", ipt2);
Intent intent = new Intent(FirstAction.this, SecondAction.class);
//把bundle设置到intent对象中
intent.putExtras(bundle);
// startActivity(intent);
/*
* Launch an activity for which you would like a result when it finished.
* When this activity exits, your onActivityResult() method will be called
* with the given requestCode. Using a negative requestCode is the same as
* calling startActivity(Intent) (the activity is not launched as a sub-activity).
* 开始SecondAction,当SecondAction结束时,可以返回结果 到FirstAction。
* 若FirstAction存在,FirstAction类中的onActivityResult()方法会被调用
*/
startActivityForResult(intent, 0);
}
});
}
// 覆盖onActivityResult方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Bundle bundle = data.getExtras();
alert("返回的数据", "返回的数据是:"+bundle.getString("result"),new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
FirstAction.this.finish();
}
});
// System.out.println(bundle.getString("result"));
}
/**
* 初始化并显示提示框
* @param massage
* @param title
* @param listener
*/
public void alert(String massage, String title,android.content.DialogInterface.OnClickListener listener) {
AlertDialog.Builder builder = new AlertDialog.Builder(FirstAction.this);
builder.setTitle(title);
builder.setMessage(massage);
builder.setPositiveButton("确定", listener);
builder.show();
}
}
第二个Activity——SecondAction.java
package com.study.activitytest;
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 SecondAction extends Activity {
private Intent intent ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
intent = this.getIntent();
//获取由FirstAction传递的数据
Bundle bundle = intent.getExtras();
String ipt1 = bundle.getString("ipt1");
String ipt2 = bundle.getString("ipt2");
TextView textView = (TextView) findViewById(R.id.TextView01);
textView.setText(ipt1+"/"+ipt2);
//设置返回的数据
bundle.putString("result", textView.getText().toString());
intent.putExtras(bundle);
Button button = (Button) findViewById(R.id.Button02);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//设定结果,并返回到FirstAction
setResult(RESULT_OK,intent);
//结束SecondAction
finish();
}
});
}
}
清单配置文件,要对两个Activity进行声明:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.study.activitytest" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/logo" android:label="@string/app_name"> <!-- FirstAction的声明 --> <activity android:name=".FirstAction" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- SecondAction的声明 --> <activity android:name=".SecondAction" android:label="@string/app_name" /> </application> </manifest>
也希望在这里认识更多android的初学者,一起学习和交流。高手,专家们,多给些提示和引导…… 谢谢。