首先创建工程
按照提示填入
我使用的是2.3版本,所以Min SDK Version填10
修改/res/layout/下main.xml文件
加入按钮
对应的程序文件如下:
< Button android:layout_height ="wrap_content"
android:layout_width ="wrap_content" android:text ="@string/showurl"
android:id ="@+id/submit_to_net" ></ Button >
这样就在页面上绘制了一个按钮,然后给按钮添加事件,就是点击后做什么
我的类信息是ActivityUse,这个类继承自Activity
文件中程序如下:
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
submit_data_tonewactivity();
}
private void submit_data_tonewactivity() {
Button button_start_browser = (Button) findViewById(R.id.submit_to_net);
button_start_browser.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
Uri myUri = Uri.parse( " http://www.baidu.com " );
Intent openBrowseIntent = new Intent(Intent.ACTION_VIEW, myUri);
startActivity(openBrowseIntent);
}
});
}
看这几句
Uri myUri = Uri.parse("http://www.baidu.com");
Intent openBrowseIntent = new Intent(Intent.ACTION_VIEW, myUri);
startActivity(openBrowseIntent);
Intent是用于多个Activity之间进行跳转的,Activity可以理解成web开发中的form.
程序调用浏览器,显示网址。
第二个例子,跳转页面并提交数据
用刚才建好的工程
复制一个main.xml并且更名为welcome.xml
配置界面如下,并且在main.xml中加入文本框和登陆按钮
welcome.xml中设置如下,需要对应修改配置属性 并在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" >
< EditText android:text ="请输入..." android:layout_height ="wrap_content"
android:layout_width ="match_parent" android:id ="@+id/logintext" ></ EditText >
< Button android:layout_height ="wrap_content"
android:layout_width ="wrap_content" android:text ="@string/exit"
android:id ="@+id/btnexit" ></ Button >
</ LinearLayout >
< EditText android:text ="请输入..." android:layout_height ="wrap_content"
android:layout_width ="match_parent" android:id ="@+id/name" ></ EditText >
< TextView android:text ="TextView" android:id ="@+id/result"
android:layout_width ="fill_parent" android:layout_height ="wrap_content" ></ TextView >
< LinearLayout android:layout_height ="wrap_content"
android:layout_width ="match_parent" android:id ="@+id/linearLayout1" >
< Button android:layout_height ="wrap_content"
android:layout_width ="wrap_content" android:text ="@string/showurl"
android:id ="@+id/submit_to_net" ></ Button >
< Button android:layout_height ="wrap_content"
android:layout_width ="wrap_content" android:text ="@string/show_login_name"
android:id ="@+id/show_login" ></ Button >
< Button android:layout_height ="wrap_content"
android:layout_width ="wrap_content" android:text ="@string/show_loginname"
android:id ="@+id/submit_to_showloginname" ></ Button >
</ LinearLayout >
Activity,需要在AndroidManifest.xml中添加设置
< activity android:name =".Welcome" android:label ="welcome" ></ activity >
Welcome.java类
public class Welcome extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super .onCreate(savedInstanceState);
setContentView(R.layout.welcome);
Bundle myBundleForGetName = this .getIntent().getExtras();
String name = myBundleForGetName.getString( " key_name " );
final EditText resultName = (EditText) findViewById(R.id.logintext);
resultName.setText( " 欢迎你 " + name);
click_button();
}
private void click_button() {
final Button btnExit = (Button) findViewById(R.id.btnexit);
btnExit.setOnClickListener(btnexit_listener);
}
// 返回到main页
private Button.OnClickListener btnexit_listener = new Button.OnClickListener() {
public void onClick(View v) {
Intent main = new Intent();
main.setClass(Welcome. this , ActivityUse. class );
startActivity(main);
}
};
}
private void submit_data_tonewactivity() {
final EditText inName = (EditText) findViewById(R.id.name);
final TextView result = (TextView) findViewById(R.id.result);
Button button_start_browser = (Button) findViewById(R.id.submit_to_net);
Button button_login = (Button) findViewById(R.id.show_login);
Button button_showLoginName = (Button) findViewById(R.id.submit_to_showloginname);
button_start_browser.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
Uri myUri = Uri.parse( " http://www.baidu.com " );
Intent openBrowseIntent = new Intent(Intent.ACTION_VIEW, myUri);
startActivity(openBrowseIntent);
}
});
button_login.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
// 接受数据
Intent openWelcomeActivityIntent = new Intent();
Bundle myBundelForName = new Bundle();
myBundelForName.putString( " key_name " , inName.getText()
.toString());
openWelcomeActivityIntent.putExtras(myBundelForName);
openWelcomeActivityIntent.setClass(ActivityUse. this ,
Welcome. class );
startActivity(openWelcomeActivityIntent);
}
});
button_showLoginName.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
result.setText(inName.getText() + " 欢迎您进入...... " );
}
});
}
注意这几句
// 接受数据
Intent openWelcomeActivityIntent = new Intent();
Bundle myBundelForName = new Bundle();
myBundelForName.putString("key_name", inName.getText()
.toString());
openWelcomeActivityIntent.putExtras(myBundelForName);
openWelcomeActivityIntent.setClass(ActivityUse.this,
Welcome.class);
startActivity(openWelcomeActivityIntent);
新用到了Bundle,这个是在对个Activity之间传递数据用的,这个例子中将信息放入的方法是putExtras
在接受端,即Welcome.java中
Bundle myBundleForGetName = this.getIntent().getExtras();
String name = myBundleForGetName.getString("key_name");
final EditText resultName = (EditText) findViewById(R.id.logintext);
resultName.setText("欢迎你" + name);
接收数据并显示,同样的方法可以传递多个值
页面样例如下:
输入111,点击登陆
跳转后的页面如下:
点击退出可以返回原页面
第三个例子,跳转页面并且得到返回值
还是用刚才的工程
加入login.xml,和Login.java文件
并在AndroidManifest.xml指定
< application android:icon ="@drawable/icon" android:label ="@string/app_name" >
< activity android:name =".ActivityUse"
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 =".Welcome" android:label ="welcome" ></ activity >
< activity android:name =".Login" android:label ="login" ></ activity >
</ application >
添加的登陆页面效果
使用的是TableLayout
login.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" >
< TableLayout android:layout_height ="wrap_content"
android:layout_width ="match_parent" android:id ="@+id/tableLayout1" >
< TableRow android:layout_height ="wrap_content"
android:layout_width ="match_parent" android:id ="@+id/tableRow1" >
< TextView android:text ="用户名" android:id ="@+id/txtName"
android:layout_width ="wrap_content" android:layout_height ="wrap_content" ></ TextView >
< EditText android:text ="" android:id ="@+id/tname"
android:layout_width ="200px" android:layout_height ="wrap_content" ></ EditText >
</ TableRow >
< TableRow android:layout_height ="wrap_content"
android:layout_width ="match_parent" android:id ="@+id/tableRow1" >
< TextView android:text ="密 码" android:id ="@+id/txtPass"
android:layout_width ="wrap_content" android:layout_height ="wrap_content" ></ TextView >
< EditText android:text ="" android:id ="@+id/tpass"
android:layout_width ="200px" android:layout_height ="wrap_content" ></ EditText >
</ TableRow >
</ TableLayout >
< LinearLayout android:layout_height ="wrap_content"
android:layout_width ="match_parent" android:id ="@+id/linearLayout1" >
< Button android:text ="登陆" android:id ="@+id/btnLogin"
android:layout_width ="115px" android:layout_height ="wrap_content" ></ Button >
< Button android:text ="取消" android:id ="@+id/btnExit"
android:layout_width ="115px" android:layout_height ="wrap_content" ></ Button >
</ LinearLayout >
</ LinearLayout >
Login.java中信息
public class Login extends Activity {
/*
* (non-Javadoc)
*
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super .onCreate(savedInstanceState);
setContentView(R.layout.login);
Button btnLogin = (Button) findViewById(R.id.btnLogin);
Button btnExit = (Button) findViewById(R.id.btnExit);
// 取值
final EditText etName = (EditText) this .findViewById(R.id.tname);
final EditText etPass = (EditText) this .findViewById(R.id.tpass);
btnLogin.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
Intent backIntent = new Intent();
Bundle stringBundle = new Bundle();
stringBundle.putString( " loginName " , etName.getText().toString());
stringBundle.putString( " logPass " , etPass.getText().toString());
backIntent.putExtras(stringBundle);
setResult(RESULT_OK, backIntent);
finish();
}
});
btnExit.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
Intent backIntent = new Intent();
setResult(RESULT_CANCELED, backIntent);
finish();
}
});
}
}
修改main.xml,增加 同时修改ActivityUse.java,并且加入get_returnvalue();函数 接受返回值通过重写
< LinearLayout android:orientation ="vertical"
android:layout_height ="wrap_content" android:layout_width ="match_parent"
android:id ="@+id/linearLayout2" >
< TextView android:text ="返回的内容显示" android:id ="@+id/textViewReturn"
android:layout_width ="fill_parent" android:layout_height ="48px" ></ TextView >
< Button android:layout_height ="wrap_content"
android:layout_width ="wrap_content" android:text ="@string/get_returnvalue"
android:id ="@+id/btnReturn" ></ Button >
</ LinearLayout >
private void get_returnvalue() {
Button btnReturn = (Button) findViewById(R.id.btnReturn);
tv = (TextView) this .findViewById(R.id.textViewReturn);
btnReturn.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
Intent toNextInt = new Intent();
toNextInt.setClass(ActivityUse. this , Login. class );
startActivityForResult(toNextInt, REQUESR_ASK);
}
});
}
/*
* 通过重载这个方法,得到返回的结果 requestCode 开启请求Intent时对应的请求码 resultCode 返回的结果验证码 data
* 返回的Intent
*
* @see android.app.Activity#onActivityResult(int, int,
* android.content.Intent)
*/
@Override
protected void onActivityResult( int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super .onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUESR_ASK) {
if (resultCode == RESULT_CANCELED) {
setTitle( " cancel...... " );
} else if (resultCode == RESULT_OK) {
showBundle = data.getExtras(); // 得到返回的包
name = showBundle.getString( " loginName " );
pass = showBundle.getString( " logPass " );
tv.setText( " 您的用户名是 " + name + " 您的密码是 " + pass);
}
}
}
需要在ActivityUse中加入,这个是设置请求,REQUESR_ASK可以设定任何值
Intent toNextInt = new Intent();
toNextInt.setClass(ActivityUse.this, Login.class);
startActivityForResult(toNextInt, REQUESR_ASK);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
在login.java端可以取值并返回
Intent backIntent = new Intent();
Bundle stringBundle = new Bundle();
stringBundle.putString("loginName", etName.getText().toString());
stringBundle.putString("logPass", etPass.getText().toString());
backIntent.putExtras(stringBundle);
setResult(RESULT_OK, backIntent);
Run一下看下结果
点击“得到返回的数据”按钮
输入信息并点击登陆
返回的结果为刚才输入的结果。