博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android使用Activity
阅读量:6986 次
发布时间:2019-06-27

本文共 11251 字,大约阅读时间需要 37 分钟。

第一个例子,显示网址

首先创建工程

2011051110262368.jpg

按照提示填入

我使用的是2.3版本,所以Min SDK Version填10

修改/res/layout/下main.xml文件

加入按钮

2011051110311966.jpg

对应的程序文件如下:

ContractedBlock.gif
ExpandedBlockStart.gif
View Code
 
<
Button
android:layout_height
="wrap_content"
android:layout_width
="wrap_content"
android:text
="@string/showurl"
android:id
="@+id/submit_to_net"
></
Button
>

这样就在页面上绘制了一个按钮,然后给按钮添加事件,就是点击后做什么

我的类信息是ActivityUse,这个类继承自Activity

文件中程序如下:

ContractedBlock.gif
ExpandedBlockStart.gif
View Code
 
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("");

    Intent openBrowseIntent = new Intent(Intent.ACTION_VIEW, myUri);
    startActivity(openBrowseIntent);

Intent是用于多个Activity之间进行跳转的,Activity可以理解成web开发中的form.

程序调用浏览器,显示网址。

2011051110402120.jpg

第二个例子,跳转页面并提交数据

用刚才建好的工程

复制一个main.xml并且更名为welcome.xml

2011051110444492.jpg

配置界面如下,并且在main.xml中加入文本框和登陆按钮

welcome.xml中设置如下,需要对应修改配置属性 并在main.xml中加入如下设置 

ContractedBlock.gif
ExpandedBlockStart.gif
View Code
 
<?
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
>
ContractedBlock.gif
ExpandedBlockStart.gif
View Code
 
<
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中添加设置

ContractedBlock.gif
ExpandedBlockStart.gif
View Code
 
<
activity
android:name
=".Welcome"
android:label
="welcome"
></
activity
>

Welcome.java类

ContractedBlock.gif
ExpandedBlockStart.gif
View Code
 
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);
}
};
}
ContractedBlock.gif
ExpandedBlockStart.gif
View Code
 
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,点击登陆

2011051111003466.jpg

跳转后的页面如下:

2011051111014560.jpg

点击退出可以返回原页面



第三个例子,跳转页面并且得到返回值

还是用刚才的工程

加入login.xml,和Login.java文件

并在AndroidManifest.xml指定 

ContractedBlock.gif
ExpandedBlockStart.gif
View Code
 
<
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

2011051111075562.jpg

login.xml中信息 

ContractedBlock.gif
ExpandedBlockStart.gif
View Code
 
<?
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中信息 

ContractedBlock.gif
ExpandedBlockStart.gif
View Code
 
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();函数 接受返回值通过重写

ContractedBlock.gif
ExpandedBlockStart.gif
View Code
 
<
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
>

ContractedBlock.gif
ExpandedBlockStart.gif
View Code
 
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一下看下结果

 

点击“得到返回的数据”按钮

2011051111195210.jpg

输入信息并点击登陆

2011051111204571.jpg

返回的结果为刚才输入的结果。

 

转载地址:http://abqpl.baihongyu.com/

你可能感兴趣的文章
python常见数据存储 csv txt pickle
查看>>
公司技术部门内部的发展变化过程。
查看>>
uda 3.C++二维向量
查看>>
Web Service概念梳理
查看>>
【FBA】SharePoint 2013自定义Providers在基于表单的身份验证(Forms-Based-Authentication)中的应用...
查看>>
C++指针和数组的区别(不能混用的情况)
查看>>
javascript学习总结1
查看>>
第三章 使用Servlet处理HTTP响应
查看>>
spring注解controller示例
查看>>
libxml中用到的Xpath语法说明
查看>>
我的cookie读写
查看>>
泛型集合中对继承类的转换
查看>>
fzu 2154 YesOrNo
查看>>
symbol MC 3090 upgrade to symbol MC 3190
查看>>
jquery之仿京东菜单
查看>>
Codeforces Round #220 (Div. 2)
查看>>
机器人的运动范围
查看>>
Linq的一些操作符-图表展示
查看>>
First java program
查看>>
codeforce#365D Free Market
查看>>