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 ...
随机推荐
- iPhone Mach-O文件格式与代码签名
错误现象1) 直接运行/Applications/MobileFonex.app/MobileFonexKilled: 9 2)gdb调试Program received signal EXC_BAD ...
- Ubuntu Server 18.04 LTS 安装
版本:Ubuntu Server 18.04.1 LTS 环境:VMware Workstation 14 Player 下载地址:https://www.ubuntu.com/download/se ...
- 补充01 Django 类视图
视图 函数视图[Function Base View] 以函数的方式定义的视图称为函数视图,函数视图便于理解.但是遇到一个视图对应的路径提供了多种不同HTTP请求方式的支持时,便需要在一个函数中编写不 ...
- pip是用代理
内网访问外网时,需要通过vpn访问,但是pip貌似不能是用vpn访问外网.直接度娘: windows下: set http_proxy=http://代理服务器:端口 不行可以试试下面的: set h ...
- Vue学习之路第十八篇:私有过滤器的使用
1.上篇已经介绍了全局过滤器的使用,“全局”顾名思义就是一次定义处处使用,可以被一个页面里不同的Vue对象所使用,如下代码所示: <body> <div id="app1& ...
- js判断当前移动设备平台
//js判断当前移动设备平台 var isiOs = false; var isAndroid = false; var isWindowsPhone = false; if(/(iPhone|iPa ...
- base64格式文件下载方法
下载图片时,接口返回的地址是base64格式的文件数据,因为页面需要把base64格式的数据转换为文件,再进行下载: 解决方案: 下载按钮: <el-button type="defa ...
- 【Codeforces Round #493 (Div. 2) B】Cutting
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然只有在前i个位置奇数偶数出现次数都相同的地方才能切. (且不管前面怎么切,这里都能切的. 那么就相当于有n个物品,每个物品的代价 ...
- 如何用Vim提高开发效率
即可 ●输入m获取到文章目录 推荐↓↓↓ C/C++编程 更多推荐<18个技术类公众微信> 涵盖:程序人生.算法与数据结构.黑客技术与网络安全.大数据技术.前端开发.Java.Python ...
- asp.net mvc--传值-后台->前台
后台传值到前台的方式 Model Binding # 这是public ActionResult中的最后部分 return View(listmode); json方式01 public void G ...