17_点击事件第四种写法_布局文件添加onclick属性
尽量不要用第四种点击事件的写法。在一万多行代码中发现了一个没被调用的代码
public void call(View v){//第四种写法参数一定是View v
//public void call(){
//在布局文件中button 声明属性onclick onclick起的名字 对应一个public void方法
//这个方法要在加载button的activity里实现 名字不能错 传入的参数必须View
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);
}
}
想半天咋整。可能是点击事件,然后还要找对应的界面。如果界面又很复杂,那就难找了。
前面三种点击事件的写法都会用到,第四种点击事件的写法就不要用了。自己写demo用第四种没问题,具体跟别人配合开发的时候第四种点击事件的写法就不要用了。
第一种写法再改改,创建一个有名的对象。
package com.itheima.dailer; 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 {
private EditText et_number; //Activity 代表了一个用户的界面. 每一个android的界面都对应一个activity
//activity可以创建一个窗口,在这个窗口上加载用户的界面(UI) 这个界面就是用来跟用户交互的
//Activity是安卓中最重要的API,
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//当activity创建的时候就会调用oncreate.在oncreate中做初始化的操作
// 首先调用setContentView()方法 加载界面 加载到内存 展示出来
setContentView(R.layout.activity_main);
et_number = (EditText) findViewById(R.id.editText1);
Button btn_call = (Button) findViewById(R.id.button1);
MyonClickListener listener = new MyonClickListener();
//给按钮添加点击事件
//btn_call.setOnClickListener(new MyonClickListener());//接收一个OnClickListener对象
btn_call.setOnClickListener(listener);
}
private class MyonClickListener implements OnClickListener { @Override
public void onClick(View v) {
int id = v.getId();
switch (key) {
case value: break; default:
break;
}
// TODO Auto-generated method stub
// 当控件被点击的时候就会调用这个onclick方法
// ① 获取用户的输入
String number = et_number.getText().toString();
// ② 判断用户输入是否为空
if(TextUtils.isEmpty(number)){//安卓提供的APITextUtils
// 如果为空 提示用户输入内容
//Toast.makeText 向界面输出一个短暂的提示
//第一个参数 上下文Context Activity就是一个上下文 可以传入当前activity对象
//第二个参数 要显示的提示文字
//第三个参数 显示提示的时间长度 Toast.LENGTH_SHORT(显示的时长较短) Toast.LENGTH_LONG(显示的时长较长)
//不要忘记要调用show()方法 显示这个Toast
Toast.makeText(MainActivity.this, "电话号码不能为空", Toast.LENGTH_SHORT).show();//duration 时长
System.out.println("用户输入是空的");
}else{
//如果不为空 用获取的电话号码 打电话
System.out.println("打电话:"+number);
//Intent 意图 就是对要进行的操作的抽象描述
Intent intent = new Intent();//创建一个意图对象
//给意图设置要操作的动作 ACTION_CALL
intent.setAction(Intent.ACTION_CALL);
//给意图设置要携带的数据
//URL 统一资源定位符 http:// ftp:// https:// abc://(访问不到,压根就没有abc这个协议),必须是已经规范好的协议
//uri 统一资源标示符 url 子类父类的关系 uri比URL多了一个特性 类似于XML和HTML的关系 uri可以自定义协议/自己声明一个协议
Uri data = Uri.parse("tel:"+number);//"tel:"就是咱们自定义的协议 不写tel:不能正确地把电话号码拿到
intent.setData(data);
//开启打电话的activity(打电话的界面)
startActivity(intent);
} } } @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;
} }
如果有多个按钮需要被点击,那么第一种写法和第三种写法都是可以的。如果是有一个按钮被点击,那么你写匿名内部类也没问题。究竟写哪种,第一种、第二种和第三种就根据自己的习惯来了。有可能很多公司都是习惯第三种写法,让当前的MainActivity来实现对应的点击事件。有的公司可能习惯于第二种写法,觉得更符合面向对象的思路。添加一个点击事件,然后这个点击事件传进一个对象。写switch会被认为是面向过程的思路。当然不同的公司习惯不一样,只要别用第四种写法就行。
<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="@string/hello_world" /> <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="18dp"
android:onClick="call"
android:text="拨打此号码" /> </RelativeLayout>
package com.itheima.fourthmathod; 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.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity { 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);
} @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;
}
//public void call1(View v){
public void call(View v){//第四种写法参数一定是View v
//public void call(){
//在布局文件中button 声明属性onclick onclick起的名字 对应一个public void方法
//这个方法要在加载button的activity里实现 名字不能错 传入的参数必须View
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);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.fourthmathod"
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.fourthmathod.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>
17_点击事件第四种写法_布局文件添加onclick属性的更多相关文章
- Android笔记---点击事件的四种写法
Android 点击事件的四种写法: 1. 以内部类的形式实现 OnClickListener 接口.定义点击事件 class MainActivity extents Activity{ // .. ...
- Android中点击事件的四种写法详解
Android中点击事件的四种写法 使用内部类实现点击事件 使用匿名内部类实现点击事件 让MainActivity实现View.OnClickListener接口 通过布局文件中控件的属性 第一种方法 ...
- [Android] 点击事件的四种写法
点击事件的必备条件:实现OnClickListener接口,重写onclick(View v)方法 以拨号简单案例为例,如下图效果: 逻辑流程: 获取点击对象,获取数据 给对象设置监听类 实现OnCl ...
- [Android]Java中点击事件的四种写法
点击事件的必备条件:实现OnClickListener接口,重写onclick(View v)方法 以拨号简单案例为例,如下图效果: 逻辑流程: 获取点击对象,获取数据 给对象设置监听类 实现OnCl ...
- 无废话Android之常见adb指令、电话拨号器、点击事件的4种写法、短信发送器、Android 中各种布局(1)
1.Android是什么 手机设备的软件栈,包括一个完整的操作系统.中间件.关键的应用程序,底层是linux内核,安全管理.内存管理.进程管理.电源管理.硬件驱动 2.Dalvik VM 和 JVM ...
- Android journey3 @点击事件的4种写法
对于android布局中的控件,如Button等会有相应的点击事件去响应它所需要的功能,今天我们就以电话拨号器的代码说明下几种点击事件: package com.itheima.phone; impo ...
- android点击事件的四种方式
android点击事件的四种方式 第一种方式:创建内部类实现点击事件 代码如下: package com.example.dail; import android.text.TextUtils; im ...
- (转)Ext.Button点击事件的三种写法
转自:http://maidini.blog.163.com/blog/static/377627042008111061844345/ ExtJs的写法太灵活了,现在收集了关于Button点击事件的 ...
- JAVA_SWT 事件的四种写法
一:匿名内部类写法 在一个组件下加入以下语句 text.addMouseListener(new MouseAdapter(){ public void mouseDoubleClich(MouseE ...
随机推荐
- HIVE 总结
http://blog.csdn.net/wisgood/article/details/17186181 常见错误 http://blog.csdn.net/sunnyyoona/article/d ...
- pygame躲敌人的游戏
#first.py# coding=utf- import pygame from pygame.locals import * from sys import exit from util impo ...
- 【leetcode】Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- [算法]打印N个数组的整体最大Top K
题目: 有N个长度不一的数组,所有的数组都是有序的,请从大到小打印这N个数组整体最大的前K个数. 例如: 输入含有N行元素的二维数组代表N个一维数组. 219,405,538,845,971 148, ...
- Luogu-4049 [JSOI2007]合金
题目中给出了三种金属的比例,实际上只用考虑两个就可以,第三个可以由另外两个确定qwq 如果把原料和需求看做二维平面上的点,可以发现两种原料能混合成的比例就在他们相连的线段上,也就是说线段上的点都能混合 ...
- spring学习(1)
struts是web框架(jsp/action/action) hibernate是orm框架,处于持久层. spring是一个框架,是容器框架.用于配置bean,并维护bean之间关系的一种框架. ...
- Oracle--存储过程和自定义函数
一.相关概念 1.存储过程和存储函数 ~指存储在数据库中供所有用户程序调用的子程序 ~存储过程和存储函数的相同点:完成特定功能的程序 ~存储过程和存储函数区别:是否用return语句返回值 2.创建和 ...
- [原]NYOJ-公约数和公倍数 -40
大学生程序代写 //http://acm.nyist.net/JudgeOnline/problem.php?pid=40 公约数和公倍数 时间限制:1000 ms | 内存限制:65535 KB ...
- uoj279温暖会指引我们前行
暖气来啦~ 动态树维护最大生成树裸题 #include<iostream> #include<cstdio> #include<cstdlib> #include& ...
- 使用 py2exe 打包 Python 程序
上回在<使用 PyInstaller 打包 Python 程序>中,我们介绍了使用 PyInstaller 对 Python 程序进行打包,今天带大家认识一个新的工具:py2exe. 接下 ...