android使用Activity
首先创建工程
按照提示填入
我使用的是2.3版本,所以Min SDK Version填10
修改/res/layout/下main.xml文件
加入按钮
对应的程序文件如下:
android:layout_width="wrap_content" android:text="@string/showurl"
android:id="@+id/submit_to_net"></Button>
这样就在页面上绘制了一个按钮,然后给按钮添加事件,就是点击后做什么
我的类信息是ActivityUse,这个类继承自Activity
文件中程序如下:
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中加入如下设置
<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>
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中添加设置
Welcome.java类
@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);
}
};
}
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指定
<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中信息
<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中信息
/*
* (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();函数 接受返回值通过重写
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>
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一下看下结果
点击“得到返回的数据”按钮
输入信息并点击登陆
返回的结果为刚才输入的结果。
android使用Activity相关推荐
- 【Android】Activity生命周期(亲测)
测试手机:Nexus 5 系统:4.4 一.测试 测试代码: 1 package com.example.androidalarm; 2 3 import android.app.Activity ...
- 【android】android中activity的生命周期
activity生命周期: 实例代码: 1 public class DemoActivity extends Activity { 2 3 //1.activity第一次被创建的时候,执行 4 @O ...
- Android中Activity的启动流程和组织管理方式(Backstack、Task)
前言: Android对Activity的组织管理会引入一些名词概念术语,像Stack.Task等,本文会做简单介绍.Activity的启动.切换以及组织管理会受不同的配置和属性的影响,本文会简单介绍 ...
- 【Android】Android 设置Activity窗体 不显示标题和全屏显示
[一]Android 设置Activity窗体 不显示标题 android:theme="@android:style/Theme.NoTitleBar" 1 <activi ...
- Android 开发 Activity里获取View的宽度和高度 转载
Android 开发 Activity里获取View的宽度和高度 转载 原文地址:https://blog.csdn.net/chenbaige/article/details/77991594 前言 ...
- 大叔也说Xamarin~Android篇~Activity之间传递数组
大叔也说Xamarin~Android篇~Activity之间传递数组 原文:大叔也说Xamarin~Android篇~Activity之间传递数组 我们在开发应用程序时,不可能只使用一个Layout ...
- 命令行下使用javah命令生成.h文件,出现“错误: 无法访问android.app.Activity 找不到android.app.Activity的类文件”的解决方法...
在学习NDK中,当我在项目的bin/classes目录下使用javah命令生成头文件时,出现了"错误: 无法访问android.app.Activity 找不到android.app.Act ...
- android图片跳转动画效果,Android实现Activity界面切换添加动画特效的方法
本文以实例形式展示了Android实现Activity界面切换添加动画特效的方法,对于Android程序设计人员来说有很好的参考借鉴价值.具体方法如下: 了解Android程序设计的人应该知道,在An ...
- Android中Activity的四种启动方式
谈到Activity的启动方式必须要说的是数据结构中的栈.栈是一种只能从一端进入存储数据的线性表,它以先进后出的原则存储数据,先进入的数据压入栈底,后进入的数据在栈顶.需要读取数据的时候就需要从顶部开 ...
- 006 Android之Activity
文章目录 四大组件之Activity Activety概述 Activity的响应事件 Activity之间的数据传递 数据传递实例 Activity的生命周期 Activity声明周期总结 Acti ...
最新文章
- 俄罗斯方块游戏笔记(一)——砖块样式配置窗体
- AI理论知识整理(14)-矩阵的秩
- 按群计数10以内_大班数学活动:按群计数
- 软件稳定性测试的测试点
- fir.im Log Guru 正式开源,快速找到 iOS 应用无法安装的原因
- python中pygame输入换行文字,Pygame:写多行
- 虚拟机以前能上网,突然上不了网
- Scrum Master需要具备哪些能力和经验
- ckEditor与ckFinder的集成
- redis,memcache二者的区别
- linux c语言 模拟键盘输入
- 智能机器人机器人心得_你是机器人吗
- 2015061410 - 推荐知乎周刊
- jpa报错:Provided id of the wrong type for class
- 微软“断臂求生”,能实现绝地反击吗?
- C#获取标准北京时间
- python 审批流系统
- win10系统改win7设置bios方法图文教程
- 5.29 C语言练习(计算数字个数:从键盘输入一串字符,输出字符串里的数字字符数。)
- android gms认证之run host test,Android GMS认证项总结
热门文章
- VTK:几何对象之Cylinder
- OpenCV使用dnn从图像中解析人体部位的实例(附完整代码)
- OpenCV全向相机校准Omnidirectional Camera Calibration
- OpenCV为轮廓创建边界框和圆
- C++ number of positive divisors计算正除数的实现算法(附完整源码)
- oracle按特定字符截取字符串_妙用字符串的替换和截取让Shell脚本精准得到你心中的那个“她”...
- centos7 tomcat_CentOS7下Tomcat应用容器抛出Too many open files问题
- mysql 必读_MYSQL 调优和使用必读
- dependencies与dependencyManagement的区(转自:http://blog.csdn.net/liutengteng130/article/details/46991829)
- 关于汉诺塔,C++代码,代码效果演算