Android studio关于点击事件后的页面跳转,选择完成后返回(onActivityResult)
我这个人喜欢直接上代码,在代码中说明更方便,更直接。
首先在.xml中设置一个button按钮,和一个EditText框,并分别做好id号。
这里我以籍贯测试对象。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_text1a1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/City"
android:text="籍贯"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTextCity"
/> </LinearLayout> 在.java中,将上面的id进行注册,初始化。
之后设置button的点击事件,intent为跳转方法
public class text1A1 extends AppCompatActivity {
private Button City;
private EditText editTextCity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text1a1);
init();
City.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(text1A1.this,text1A2.class);
startActivityForResult(intent,0);
}
});
}
使用onActivityResult方法,对值的调用并返回,这里我设置“giguan”为值的接口返回
protected void onActivityResult(int A1,int A2,Intent data){
super.onActivityResult(A1,A2,data);
if (A1 == 0 && A2 ==0);
Bundle extras=data.getExtras();
String giguan =extras.getString("giguan");
editTextCity.setText(giguan);
}
private void init() {
City=(Button)findViewById(R.id.City);
editTextCity=(EditText)findViewById(R.id.editTextCity);
}
}
新建textA2,在textA2的.xml表中建立ListView控件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_text1_a2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"> </ListView> </LinearLayout> 在textA2.java表中,依然注册控件,初始化。对应前面的接口,创建数据
public class text1A2 extends AppCompatActivity {
private ListView listView;
private String[] giguan={"非洲","欧洲","亚洲"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text1_a2);
init();
ArrayAdapter adapter =new ArrayAdapter(text1A2.this,
android.R.layout.simple_list_item_1,giguan);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent =getIntent();
Bundle bundle =new Bundle();
bundle.putString("giguan",giguan[position]);
intent.putExtras(bundle);
text1A2.this.setResult(0,intent);
text1A2.this.finish();
}
});
}
private void init() {
listView=(ListView)findViewById(R.id.listView);
}
}
Android studio关于点击事件后的页面跳转,选择完成后返回(onActivityResult)的更多相关文章
- android 学习第一天 了解事件机制,页面跳转等常用操作
点击时间2种 第一种,通过初始化页面 写入点击事件 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedI ...
- 点击<a>标签后禁止页面跳至顶部
一.点击<a>标签后禁止页面跳至顶部 1. 使用 href="javascript:void(0);",例如: <a href="javascript: ...
- Android ListView item 点击事件失效问题的解决
关于ListView点击无效,item无法相应点击事件的问题,网上有很多, 大致可分为俩种情况, 一种是 item中存在 ImageButton 等可以点击的组件,这会抢先获得ListView的焦点. ...
- Android笔记——Button点击事件几种写法
Button点击事件:大概可以分为以下几种: 匿名内部类 定义内部类,实现OnClickListener接口 定义的构造方法 用Activity实现OnClickListener接口 指定Button ...
- Android四种点击事件和五中存储方式
Android点击事件的四种实现方式 1.内部类实现onClickListenter接口 bt_login.setOnClickListener(new MyListener()); class My ...
- Android多次点击事件的监听和处理
package com.example.administrator.mystudent.MoreClick; import android.os.SystemClock; import android ...
- Android监听点击事件实现的三种方法
监听点击事件实现的三种方法:1.匿名内部类2.外部类3.直接实现接口 1.匿名内部类: package com.jereh.calculator; import android.content.Con ...
- Android学习-----Button点击事件几种写法
Button点击事件:大概可以分为以下几种: 匿名内部类 定义内部类,实现OnClickListener接口 定义的构造方法 用Activity实现OnClickListener接口 指定Button ...
- Android开发之点击事件(Button)
Button点击事件 创建项目: 1.Fiel-------->New ------->Android Application Project 2.将Form Widght 文件中的But ...
随机推荐
- matlab学习GUI的基本操作
在命令行窗口输入guide 单击确定后出现,可以选择控件来设计,然后可以保存 右击可以查看控件的所有属性 举一个简单的GUI实例---实现数据的传输 添加可编辑文本框 更改字体大小,string默认的 ...
- vc++创建窗体
//创建窗口,一般分为四步 /*1 WinMain函数的定义 2 创建一个窗口 3 编写消息循环 4 编写窗口过程函数*/ #include <windows.h> //包含windows ...
- Win7系统无法记住网络访问的凭据怎么办?
访问内网其他IP时已经打勾记住我的凭据了,但是注销或者重启Win7系统之后,又得重新输入用户名跟密码怎么办? 下面就为大家介绍一下如何让Win7系统记住网络访问的凭据. 以上问题还会引发,重启后无法正 ...
- python3发送邮件
import smtplib from email.mime.text import MIMEText from email.utils import formataddr import psutil ...
- [bzoj3505 Cqoi2014] 数三角形 (容斥+数学)
传送门 Description 给定一个nxm的网格,请计算三点都在格点上的三角形共有多少个.下图为4x4的网格上的一个三角形. 注意三角形的三点不能共线. Input 输入一行,包含两个空格分隔的正 ...
- 【hiho一下 第八周】状态压缩·一
[题目链接]:http://hihocoder.com/problemset/problem/1044 [题意] [题解] 设f[i][j]表示; 前i-1个位置已经决策完; 然后i-m+1..i这一 ...
- PHP学习总结(10)——PHP入门篇之自定义网站根目录
- SSH框架整合截图(二)
客户拜访管理 1 什么是客户拜访 (1)客户:与公司有业务往来的 (2)用户:可以使用系统的人 2 用户和客户关系 (1)用户和客户之间是拜访的关系 (2)用户 和 客户 是 多对多关系 ** 一个用 ...
- 启动 Appium 自带模拟器
1.先在sclipse中新建并打开一个设备 2.启动appium 3.安装apk 打开cmd 并在sdk安装目录的tools文件夹下输入安装命令adb install xxx.apk(在这之前需要把 ...
- Mysql提升大数据导入速度的绝妙方法
一.对于Myisam类型的表,可以通过以下方式快速的导入大量的数据. ALTER TABLE tblname DISABLE KEYS; loading the data A ...