Android:后台给button绑定onClick事件、当返回项目到手机页面时提示是否退出APP
上一篇文章我们学习了android通过findViewById的方式查找控件,本章将了解button控件,及btton如何绑定控件。
通过android的ui设计工具设计一个登录页面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <TextView
android:id="@+id/tvUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/etPassword"
android:layout_alignParentLeft="true"
android:text="@string/main_activity_username" /> <TextView
android:id="@+id/tvPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/etPassword"
android:layout_alignBottom="@+id/etPassword"
android:layout_alignLeft="@+id/tvUsername"
android:text="@string/main_activity_password" /> <EditText
android:id="@+id/etUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="42dp"
android:layout_toRightOf="@+id/tvUsername"
android:hint="@string/main_activity_username_hint"
android:ems="10" /> <EditText
android:id="@+id/etPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etUsername"
android:layout_below="@+id/etUsername"
android:layout_marginTop="41dp"
android:hint="@string/main_activity_password_hint"
android:ems="10"
android:inputType="textPassword" > <requestFocus />
</EditText> <Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btnOk"
android:layout_alignRight="@+id/etUsername"
android:layout_marginRight="57dp"
android:text="@string/main_activity_btnCancel" /> <Button
android:id="@+id/btnOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/etPassword"
android:layout_marginRight="32dp"
android:layout_marginTop="59dp"
android:layout_toLeftOf="@+id/btnCancel"
android:text="@string/main_activity_btnOk" /> <TextView
android:id="@+id/tvShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvPassword"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/etUsername"
android:layout_marginBottom="74dp"
android:text="" /> </RelativeLayout>
1)文本框使用的EditText控件;
2)文本hint是输入提示;
3)inputType是文本内容的格式设定。
后台给button绑定onClick事件:
android后台绑定button的onClick的函数setOnClickListener函数:
package com.example.helloword; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity {
private EditText etUsername, etPassword;
private TextView tvUsername, tvPassword, tvShow;
private Button btnOk, btnCancel; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); etUsername = (EditText) this.findViewById(R.id.etUsername);
etPassword = (EditText) this.findViewById(R.id.etPassword);
tvUsername = (TextView) this.findViewById(R.id.tvUsername);
tvPassword = (TextView) this.findViewById(R.id.tvPassword);
tvShow = (TextView) this.findViewById(R.id.tvShow);
btnOk = (Button) this.findViewById(R.id.btnOk);
btnCancel = (Button) this.findViewById(R.id.btnCancel); btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String username = etUsername.getText().toString();
String password = etPassword.getText().toString(); tvShow.setText(username + "," + password);
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
etUsername.setText("");
etPassword.setText("");
tvShow.setText("");
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
当返回项目到手机页面时提示是否退出APP
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// 当点击回退时,弹出该窗口(也就相当于关闭操作)
if (keyCode == KeyEvent.KEYCODE_BACK) {
new AlertDialog.Builder(this).setTitle("是否退出?")
.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
finish();
}
}).setNegativeButton("取消", null).show();
return true;
}
return super.onKeyUp(keyCode, event);
}
使用android仿真机测试效果:

当点击返回到手机时:

Android:后台给button绑定onClick事件、当返回项目到手机页面时提示是否退出APP的更多相关文章
- ListView中的组件Button的OnClick事件触发时机
Android开发时,ListView中的组件Button的OnClick事件必须在ListView之外的组件事件触发后才能触发? 此处ListView无OnItemClick事件,而且ListVie ...
- input 的blur事件之后button的onclick事件不执行解决方案
最近发现网页程序中有个BUG,就是在input标签输入框中输入完数据后,直接点击“取消” 按钮的时候.出现网页崩死的情况: 经过小主酸菜我,各种方法的尝试后,找到一个初步可以解决的方案,在这里分享给大 ...
- Android开发入门——Button绑定监听事件三种方式
import android.app.Activity; import android.os.Bundle;import android.view.View;import android.widget ...
- Asp.Net回车键触发Button的OnClick事件解决方案
在aspx页面有textbox文本框,还有三个button按钮.启用textbox的TextChanged事件和button的click事件. 问题: 现在在textbox文本框输入完数据按“回车”后 ...
- 在js中绑定onclick事件为什么不加括号,在html代码中必须要加?
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 问题解决:在js中绑定onclick事件为什么不加括号,在html代码中必须要加?(转载)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- android ListView中button点击事件盖掉onItemClick解决办法
ListView 1.在android应用当中,很多时候都要用到listView,但如果ListView当中添加Button后,ListView 自己的 public void onItemClick ...
- button的onclick事件给函数传递参数
ul+='<button onclick="pay(\''+regiId+'\')" >按钮</button>' //此为原生JS页面拼接//此方式的关键就 ...
- 页面所有的button绑定同一个事件,点击不同的button赋值不同
<script type="text/javascript"> $(function(){ $("input[type='button']").cl ...
随机推荐
- Maven-07: 插件的自定义绑定
除了内置绑定以外,用户还能够自己选择将某个插件目标绑定到生命周期的某个阶段上,这种自定义绑定方式能让Maven项目在构建过程中执行更多更富特色的任务. 一个常见的例子是创建项目的源码jar包.内置的插 ...
- 笔记:Hibernate 框架配置说明
下载 Hibernate ,打开地址 www.hibernate.org ,点击 Hibernate ORM -> Downloads 下载 4.3.11 版本,要使用Hibernate 需要把 ...
- poj-2909-哥德巴赫猜想
Description For any even number n greater than or equal to 4, there exists at least one pair of prim ...
- js实现单双行文本溢出添加省略号
# 单双行文本溢出省略 ``` // 2. 当内容过多的时候,单行省略号: overflow: hidden; text-overflow:ellipsis; white-space: nowrap; ...
- python(练习实例)
Python 练习实例1 题目:有四个数字:1.2.3.4,能组成多少个互不相同且无重复数字的三位数?各是多少? 我的代码:python 3+ #2017-7-20 list_h = [1,2,3,4 ...
- markdown语法小结
引用数学公式1 \[ \begin{equation} \pi^2=x^2+y \label{eq_lab1} \end{equation} \] Here we cite this equation ...
- js浮点数运算的坑,多少同学有碰到过?
javascript中的数字都是双精度的浮点数. JavaScript中的整数并不是一个独立的数据类型,而是浮点数的一个子集. 浮点数的坑我们看下面的例子 在浏览器的console 控制台上我们分别进 ...
- ConcurrentHashMap 源码分析
ConcurrentHashMap 源码分析 1. 前言 终于到这个类了,其实在前面很过很多次这个类,因为这个类代码量比较大,并且涉及到并发的问题,还有一点就是这个代码有些真的晦涩,不好懂.前前 ...
- 团队作业4——第一次项目冲刺(Alpha版本)
第一天http://www.cnblogs.com/ThinkAlone/p/7861070.html 第二天http://www.cnblogs.com/ThinkAlone/p/7861191.h ...
- python网络爬虫,知识储备,简单爬虫的必知必会,【核心】
知识储备,简单爬虫的必知必会,[核心] 一.实验说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou 2. 环境介绍 本实验环境采用带桌面的Ubuntu Linux环境,实验中会用到桌 ...