16_点击事件第三种写法_activity实现接口

第一种写法是有名内部类,第二种写法是匿名内部类,第三种写法是MainActivity实现接口OnClickListener.直接让MainActivity实现了OnClickListener这个接口。

用当前的MainActivity来实现这个接口.这种实现方式要在点击事件里面区分究竟是哪一个按钮被点了.
public void onClick(View v) {
哪一个控件被点击,这个控件就会被作为参数传进来.
v.getId()
拿到这个控件对应的具体的id是啥.由于每个控件的id都不一样,由于每个控件它的id都不一样.咱们就可以通过这个id进行区分.就可以知道是哪一个按钮被点击了。
package com.itheima.thirdclickmethod; import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener{ private EditText et_number; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//把布局文件加载到界面上
setContentView(R.layout.activity_main);
et_number = (EditText) findViewById(R.id.editText1);
Button btn_call = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button2);
Button btn3 = (Button) findViewById(R.id.button3);
btn_call.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
} @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;
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
//View v 参数 当控件被点击的时候 被点击的对象就会作为参数传进来 可以通过控件的id进行区分
int id = v.getId();//
switch (id) {
case R.id.button1:
String number = et_number.getText().toString().trim();
if(TextUtils.isEmpty(number)){
Toast.makeText(this, "输入不能为空", Toast.LENGTH_SHORT).show();
}else{
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+number));
startActivity(intent);//通过这个意图开启打电话的界面
}
break;
case R.id.button2:
Toast.makeText(this, "按钮2被点击", Toast.LENGTH_SHORT).show();
break;
case R.id.button3:
Toast.makeText(this, "按钮3被点击", Toast.LENGTH_SHORT).show();
break; default:
break;
} } }
<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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入电话" /> <EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="14dp"
android:hint="在此输入电话"
android:ems="10" > <requestFocus />
</EditText> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="22dp"
android:text="拨打此号码" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="24dp"
android:text="Button2" /> <Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:layout_marginTop="29dp"
android:text="Button3" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.thirdclickmethod"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CALL_PHONE"/> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.thirdclickmethod.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
16_点击事件第三种写法_activity实现接口的更多相关文章
- (转)Ext.Button点击事件的三种写法
转自:http://maidini.blog.163.com/blog/static/377627042008111061844345/ ExtJs的写法太灵活了,现在收集了关于Button点击事件的 ...
- JavaScript 点击事件的三种写法
嵌入式 <button οnclick='alert("hello")'>点击按钮</button> 脚本模型 btn.onclick=function() ...
- jquery绑定点击事件的三种写法
一.用jquery动态绑定点击事件的写法 部分代码: <script type="text/javascript"> $(document).ready(functio ...
- Android journey3 @点击事件的4种写法
对于android布局中的控件,如Button等会有相应的点击事件去响应它所需要的功能,今天我们就以电话拨号器的代码说明下几种点击事件: package com.itheima.phone; impo ...
- Android中点击事件的四种写法详解
Android中点击事件的四种写法 使用内部类实现点击事件 使用匿名内部类实现点击事件 让MainActivity实现View.OnClickListener接口 通过布局文件中控件的属性 第一种方法 ...
- 无废话Android之常见adb指令、电话拨号器、点击事件的4种写法、短信发送器、Android 中各种布局(1)
1.Android是什么 手机设备的软件栈,包括一个完整的操作系统.中间件.关键的应用程序,底层是linux内核,安全管理.内存管理.进程管理.电源管理.硬件驱动 2.Dalvik VM 和 JVM ...
- Android笔记---点击事件的四种写法
Android 点击事件的四种写法: 1. 以内部类的形式实现 OnClickListener 接口.定义点击事件 class MainActivity extents Activity{ // .. ...
- 17_点击事件第四种写法_布局文件添加onclick属性
尽量不要用第四种点击事件的写法.在一万多行代码中发现了一个没被调用的代码 public void call(View v){//第四种写法参数一定是View v //public void call( ...
- [Android] 点击事件的四种写法
点击事件的必备条件:实现OnClickListener接口,重写onclick(View v)方法 以拨号简单案例为例,如下图效果: 逻辑流程: 获取点击对象,获取数据 给对象设置监听类 实现OnCl ...
随机推荐
- HDU - 1728 逃离迷宫 【BFS】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1728 思路 BFS 一开始 从开始位置 往四周走 如果能走的话 这个时候 转弯次数都是0 我们的标记不 ...
- ubuntu14.04 在自带python2.7上安装python3.3.5 可以用但是有问题
一开始写的时候并没有发现这么安装有问题,后来发现问题也不想删了,当个教训,如果想安装从python自带版本换别的版本的话就别接着看了,这么安装有问题.需要进行配置,但是我还不会.其实下面只是差了一步配 ...
- StartUML如何画“不折弯”的直线
将下图中line style改成 OBlique.快捷键Ctrl +B
- 数据库连接理解——JDBC
需求:数据库操作 数据是:用户信息 1.连接数据库 JDBC Hibernate 2.操作数据库 c create r read u update d delete 3.关闭数据库连接 interf ...
- Data Structure Stack: Infix to Postfix
http://geeksquiz.com/stack-set-2-infix-to-postfix/ #include <iostream> #include <vector> ...
- jps参数
jps是jdk1.5提供的用来显示当前java进程的指令,能显示进程id及进程名称(多指jar包名称.class名称) -q 只显示进程ID -m 显示传递给main方法的参数 -l 显示应用程序完整 ...
- django配置文件环境分离后celery的启动方式整理
django项目中,当配置文件分离时: 启动方式1: 硬编码写死在manage.py中: os.environ.setdefault("DJANGO_SETTINGS_MODULE" ...
- java入门了解14
GUI 1.分类: 一.AWT(Abstract Window Toolkit):抽象窗体工具集 java.awt.*适合做简单的图像用户界面,复杂的不行,基于底层操作系统,所以不同的操作系统显示的界 ...
- nginx源码中upstream的主要流程
upstream 即上游的意思,是一个想对到概念,从客户端到中间的网络链路到服务器到链路中,可以将越接近客户到设备越理解成下游,相反到为上游,所以如果只有一个upstream,可以将其为理解成转发客户 ...
- Java -- 内部类, 成员内部类,局部内部类,匿名内部类,闭包和回调, 枚举类
1. 成员内部类分为 静态内部类 和 非静态内部类. 非静态内部类 和 外部类的其他成员一样处理, 非静态内部类可以访问外部类的private的属性,而外部类不能访问非静态内部类的属性,需要实例非静 ...