android使用Activity

简介:
第一个例子,显示网址

首先创建工程

按照提示填入

我使用的是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一下看下结果

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

输入信息并点击登陆

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


目录
相关文章
|
7月前
|
Android开发
Android面试题之Activity的启动模式和flag
Android Activity的四种启动模式:standard(默认,每次启动创建新实例),singleTop(栈顶复用,不走onCreate,调用onNewIntent),singleTask(栈内唯一,清除上方Activity)和singleInstance(单独栈内唯一)。启动模式在AndroidManifest.xml中配置,Intent Flags如FLAG_ACTIVITY_CLEAR_TOP和FLAG_ACTIVITY_SINGLE_TOP可实现类似功能。了解这些对于处理Activity栈管理至关重要。
70 0
|
4月前
|
Android开发
Android面试之Activity启动流程简述
Android面试之Activity启动流程简述
111 6
|
4月前
|
消息中间件 Android开发 索引
Android面试高频知识点(4) 详解Activity的启动流程
Android面试高频知识点(4) 详解Activity的启动流程
41 3
|
4月前
|
缓存 前端开发 Android开发
Android实战之如何截取Activity或者Fragment的内容?
本文首发于公众号“AntDream”,介绍了如何在Android中截取Activity或Fragment的屏幕内容并保存为图片。包括截取整个Activity、特定控件或区域的方法,以及处理包含RecyclerView的复杂情况。
41 3
|
9月前
|
XML Java Android开发
利用Bundle实现Android Activity间消息的传递
利用Bundle实现Android Activity间消息的传递
78 2
|
4月前
|
Android开发
Android面试之Activity启动流程简述
Android面试之Activity启动流程简述
33 0
|
5月前
|
消息中间件 Android开发 索引
Android面试高频知识点(4) 详解Activity的启动流程
讲解Activity的启动流程了,Activity的启动流程相对复杂一下,涉及到了Activity中的生命周期方法,涉及到了Android体系的CS模式,涉及到了Android中进程通讯Binder机制等等, 首先介绍一下Activity,这里引用一下Android guide中对Activity的介绍:
85 4
|
6月前
|
XML Android开发 数据格式
android中两个Activity同时设定了intent-filter的category为android.intent.category.LAUNCHER,会发生什么情况?
本文通过案例分析了在Android中当两个Activity都设置了`android.intent.category.LAUNCHER`类别时,会导致它们同时在应用启动器的"所有应用"页面显示为不同的启动入口。
188 2
android中两个Activity同时设定了intent-filter的category为android.intent.category.LAUNCHER,会发生什么情况?
|
5月前
|
Android开发 开发者
Android面试之Activity启动流程简述
每个Android开发者都熟悉的Activity,但你是否了解它的启动流程呢?本文将带你深入了解。启动流程涉及四个关键角色:Launcher进程、SystemServer的AMS、应用程序的ActivityThread及Zygote进程。核心在于AMS与ActivityThread间的通信。文章详细解析了从Launcher启动Activity的过程,包括通过AIDL获取AMS、Zygote进程启动以及ActivityThread与AMS的通信机制。接着介绍了如何创建Application及Activity的具体步骤。整体流程清晰明了,帮助你更深入理解Activity的工作原理。
88 0
|
6月前
|
开发工具 Android开发
解决Manifest merger failed : android:exported needs to be explicitly specified for <activity>
解决Manifest merger failed : android:exported needs to be explicitly specified for <activity>
161 1

热门文章

最新文章

  • 1
    如何修复 Android 和 Windows 不支持视频编解码器的问题?
  • 2
    【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
  • 3
    当flutter react native 等混开框架-并且用vscode-idea等编译器无法打包apk,打包安卓不成功怎么办-直接用android studio如何打包安卓apk -重要-优雅草卓伊凡
  • 4
    【04】flutter补打包流程的签名过程-APP安卓调试配置-结构化项目目录-完善注册相关页面-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程
  • 5
    APP-国内主流安卓商店-应用市场-鸿蒙商店上架之必备前提·全国公安安全信息评估报告如何申请-需要安全评估报告的资料是哪些-优雅草卓伊凡全程操作
  • 6
    【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
  • 7
    Android经典面试题之Kotlin中Lambda表达式和匿名函数的区别
  • 8
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 9
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
  • 10
    Android 13 SystemUI 启动流程
  • 1
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    24
  • 2
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
    21
  • 3
    APP-国内主流安卓商店-应用市场-鸿蒙商店上架之必备前提·全国公安安全信息评估报告如何申请-需要安全评估报告的资料是哪些-优雅草卓伊凡全程操作
    52
  • 4
    【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
    35
  • 5
    当flutter react native 等混开框架-并且用vscode-idea等编译器无法打包apk,打包安卓不成功怎么办-直接用android studio如何打包安卓apk -重要-优雅草卓伊凡
    70
  • 6
    【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
    112
  • 7
    Android经典面试题之Kotlin中Lambda表达式和匿名函数的区别
    29
  • 8
    如何修复 Android 和 Windows 不支持视频编解码器的问题?
    265
  • 9
    【04】flutter补打包流程的签名过程-APP安卓调试配置-结构化项目目录-完善注册相关页面-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程
    75
  • 10
    【02】写一个注册页面以及配置打包选项打包安卓apk测试—开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈
    36