点击事件的4种写法
来源:互联网 发布:超图软件目标价 编辑:程序博客网 时间:2024/06/14 10:33
xml:(1,2,3公用)
<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" > <EditText android:id="@+id/et_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="phone" /> <Button android:id="@+id/btn_call" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/et_number" android:text="call" /></RelativeLayout>
java:
<pre name="code" class="java">package org.gentry.demo;import android.net.Uri;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.text.TextUtils;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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); // 加载布局文件Button btn_call = (Button) findViewById(R.id.btn_call); // 查找到按钮et_number = (EditText) findViewById(R.id.et_number); // 查找文本框// 给按钮注册点击事件// 1.创建一个内部类,定义点击事件btn_call.setOnClickListener(new MyListener());}private class MyListener implements OnClickListener {/* * 当按钮被点击的时候调用的方法 * * @see android.view.View.OnClickListener#onClick(android.view.View) */@Overridepublic void onClick(View v) {// TODO Auto-generated method stubcallPhone();}}/* * (1)设置提醒信息 * * (2)获取打电话的意图 */private void callPhone() {String number = et_number.getText().toString().trim(); // 获取et_number中的内容// 号码为空时弹出提醒信息if (TextUtils.isEmpty(number)) {Toast.makeText(MainActivity.this, "number is empty",Toast.LENGTH_SHORT).show();return;}Intent intent = new Intent(); // 意图:想干一件什么事情intent.setAction(Intent.ACTION_CALL); // 动作:打电话intent.setData(Uri.parse("tel:" + number)); // 数据:电话号码(Uri:统一资源标示符)startActivity(intent); // 开启意图}}
2. 采用匿名内部类创建点击事件
java:
<pre name="code" class="java">package org.gentry.demo;import android.net.Uri;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.text.TextUtils;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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); // 加载布局文件Button btn_call = (Button) findViewById(R.id.btn_call); // 查找到按钮et_number = (EditText) findViewById(R.id.et_number); // 查找文本框// 给按钮注册点击事件// 2.采用匿名内部类创建点击事件btn_call.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubcallPhone();}});}/* * (1)设置提醒信息 * * (2)获取打电话的意图 */private void callPhone() {String number = et_number.getText().toString().trim(); // 获取et_number中的内容// 号码为空时弹出提醒信息if (TextUtils.isEmpty(number)) {Toast.makeText(MainActivity.this, "number is empty",Toast.LENGTH_SHORT).show();return;}Intent intent = new Intent(); // 意图:想干一件什么事情intent.setAction(Intent.ACTION_CALL); // 动作:打电话intent.setData(Uri.parse("tel:" + number)); // 数据:电话号码(Uri:统一资源标示符)startActivity(intent); // 开启意图}}
3. 让activity实现点击事件的方法
java:
<pre name="code" class="java">package org.gentry.demo;import android.net.Uri;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.text.TextUtils;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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); // 加载布局文件Button btn_call = (Button) findViewById(R.id.btn_call); // 查找到按钮et_number = (EditText) findViewById(R.id.et_number); // 查找文本框// 给按钮注册点击事件// 3.让activity实现点击事件的接口btn_call.setOnClickListener(this);}/* * 接口里面未实现的方法 */@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.btn_call:callPhone();break;default:break;}}/* * (1)设置提醒信息 * * (2)获取打电话的意图 */private void callPhone() {String number = et_number.getText().toString().trim(); // 获取et_number中的内容// 号码为空时弹出提醒信息if (TextUtils.isEmpty(number)) {Toast.makeText(MainActivity.this, "number is empty",Toast.LENGTH_SHORT).show();return;}Intent intent = new Intent(); // 意图:想干一件什么事情intent.setAction(Intent.ACTION_CALL); // 动作:打电话intent.setData(Uri.parse("tel:" + number)); // 数据:电话号码(Uri:统一资源标示符)startActivity(intent); // 开启意图}}
4. 在布局文件里绑定一个点击的方法
xml:
<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" > <EditText android:id="@+id/et_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="phone" /> <Button android:onClick="callButtonClicked" android:id="@+id/btn_call" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/et_number" android:text="call" /></RelativeLayout>
java:
<pre name="code" class="java">package org.gentry.demo;import android.net.Uri;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.text.TextUtils;import android.view.View;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {private EditText et_number;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); // 加载布局文件et_number = (EditText) findViewById(R.id.et_number); // 查找文本框}/* * 给按钮注册点击事件 * * 4.在布局文件里面绑定一个点击的方法 */public void callButtonClicked(View view) {callPhone();}/* * (1)设置提醒信息 * * (2)获取打电话的意图 */private void callPhone() {String number = et_number.getText().toString().trim(); // 获取et_number中的内容// 号码为空时弹出提醒信息if (TextUtils.isEmpty(number)) {Toast.makeText(MainActivity.this, "number is empty",Toast.LENGTH_SHORT).show();return;}Intent intent = new Intent(); // 意图:想干一件什么事情intent.setAction(Intent.ACTION_CALL); // 动作:打电话intent.setData(Uri.parse("tel:" + number)); // 数据:电话号码(Uri:统一资源标示符)startActivity(intent); // 开启意图}}
0 0
- Android点击事件的4种写法
- 点击事件的4种写法
- 点击事件的4种写法
- Android点击事件的4种写法
- Android点击事件的4种写法
- Android 点击事件的4种写法
- 点击事件的4种写法
- Android按钮的点击事件的4种写法
- android button点击事件的4种写法
- android常用的4种点击事件写法
- android_02_按钮点击事件的4种写法
- Android 响应按钮点击事件的4种写法
- Android点击事件的四种写法
- android点击事件的四种写法
- 点击事件的四种写法
- 点击事件的几种写法
- Android点击事件的四种写法
- Android点击事件的四种写法
- 待字闺中之又见Google之星分析
- .NET/C#/Oracle数据库操作类
- 分治算法思想
- Delphi7框架中的Label.Caption有时候不显示的问题
- 就个环境规划局规划局和各家各户规范复合风管话费
- 点击事件的4种写法
- 动态规划6-背包问题+记忆递归
- 几个环节个环境规划就的风和风格和风格化符合规范化
- List<> 实现模糊查询
- 漫漫测试路
- 规划局规划局很关键的方风格和风格化风格和风格化
- InstallAnywhere web打包成exe文件(tomcat6 , mysql。jdk1.6)
- 家和国家各环节的风格的风格的发给的非官方大哥
- 线性表的操作