Android点击事件的4种写法
来源:互联网 发布:ecshop换模板sql查询 编辑:程序博客网 时间:2024/05/16 10:27
activity_main.xml
1、2、3三种方法都是用上面的UI界面
MainActivity.java
<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="com.example.phonecall.MainActivity" > <EditText android:id="@+id/call_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="23dp" android:ems="10" android:inputType="phone" > <requestFocus /> </EditText> <Button android:id="@+id/dial" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/call_phone" android:layout_below="@+id/call_phone" android:layout_marginTop="18dp" android:text="@string/dial" /> </RelativeLayout>
1、2、3三种方法都是用上面的UI界面
1、创建一个内部类 定义点击事件
MainActivity.java
package com.example.phonecall;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.text.TextUtils;import android.view.Menu;import android.view.MenuItem;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 ActionBarActivity{private Button dial = (Button) this.findViewById(R.id.dial);;private EditText call_number = (EditText) this.findViewById(R.id.call_phone); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dial.setOnClickListener(new MyListener()); }<pre name="code" class="java"> private MyListener implements OnClickListener{ String number = call_number.getText().toString().trim();if (TextUtils.isEmpty(number)) {Toast.makeText(MainActivity.this, "号码不能为空", Toast.LENGTH_SHORT).show();return;}//意图干一件什么事情Intent intent = new Intent();intent.setAction(Intent.ACTION_CALL);intent.setData(Uri.parse("tel:"+number));startActivity(intent);break; } @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 boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}
2、采用匿名内部类
MainActivity.java
package com.example.phonecall;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.text.TextUtils;import android.view.Menu;import android.view.MenuItem;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 ActionBarActivity{private Button dial = (Button) this.findViewById(R.id.dial);;private EditText call_number = (EditText) this.findViewById(R.id.call_phone); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dial.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub String number = call_number.getText().toString().trim(); if (TextUtils.isEmpty(number)) {Toast.makeText(MainActivity.this, "号码不能为空", Toast.LENGTH_SHORT).show();return;} //意图干一件什么事情 Intent intent = new Intent(); intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:"+number)); 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; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}
3、接口里面为实现的方法(常用写法)
MainActivity.java
package com.example.phonecall;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.text.TextUtils;import android.view.Menu;import android.view.MenuItem;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 ActionBarActivity implements OnClickListener{private Button dial = (Button) this.findViewById(R.id.dial);;private EditText call_number = (EditText) this.findViewById(R.id.call_phone); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); <span style="color:#ff0000;"> dial.setOnClickListener(this);</span> } @Overridepublic void onClick(View v) {// TODO Auto-generated method stub switch (v.getId()) {case R.id.call_phone:String number = call_number.getText().toString().trim();if (TextUtils.isEmpty(number)) {Toast.makeText(MainActivity.this, "号码不能为空", Toast.LENGTH_SHORT).show();return;}//意图干一件什么事情Intent intent = new Intent();intent.setAction(Intent.ACTION_CALL);intent.setData(Uri.parse("tel:"+number));startActivity(intent);break;default:break;}} @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 boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}
4、在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="com.example.phonecall.MainActivity" > <EditText android:id="@+id/call_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="23dp" android:ems="10" android:inputType="phone" > <requestFocus /> </EditText> <Button <span style="color:#ff0000;">android:onClick="dialButtonClicked"</span> android:id="@+id/dial" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/call_phone" android:layout_below="@+id/call_phone" android:layout_marginTop="18dp" android:text="@string/dial" /> </RelativeLayout>
MainActivity.java
package com.example.phonecall;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.text.TextUtils;import android.view.Menu;import android.view.MenuItem;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 ActionBarActivity{private EditText call_number = (EditText) this.findViewById(R.id.call_phone); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } private MyListener implements OnClickListener{ String number = call_number.getText().toString().trim();if (TextUtils.isEmpty(number)) {Toast.makeText(MainActivity.this, "号码不能为空", Toast.LENGTH_SHORT).show();return;}//意图干一件什么事情Intent intent = new Intent();intent.setAction(Intent.ACTION_CALL);intent.setData(Uri.parse("tel:"+number));startActivity(intent);break; } public void dialButtonClicked(View v) { String number = call_number.getText().toString().trim();if (TextUtils.isEmpty(number)) {Toast.makeText(MainActivity.this, "号码不能为空", Toast.LENGTH_SHORT).show();return;}//意图干一件什么事情Intent intent = new Intent();intent.setAction(Intent.ACTION_CALL);intent.setData(Uri.parse("tel:"+number));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; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}
0 0
- Android点击事件的4种写法
- Android点击事件的4种写法
- Android点击事件的4种写法
- Android 点击事件的4种写法
- Android点击事件的四种写法
- android点击事件的四种写法
- Android点击事件的四种写法
- Android点击事件的四种写法
- android点击事件的四种写法
- Android点击事件的三种写法
- [Android] 点击事件的四种写法
- Android----点击事件的四种写法
- Android按钮的点击事件的4种写法
- android button点击事件的4种写法
- android常用的4种点击事件写法
- Android 响应按钮点击事件的4种写法
- Android 点击事件 4种 写法
- 点击事件的4种写法
- SSH框架中struts开发环境搭建
- 学习Golang语言(2)
- 想的太复杂
- android 四大组件之BroadcaseReceiver
- swift ios8 定位
- Android点击事件的4种写法
- 记录学习的过程
- Windows 8.1, UEFI 模式下U盘安装Ubuntu 14.10 或者 Opensuse 13.02 双系统
- 常见随机分布的数学期望及方差
- 黑马程序员——杂记——天道酬勤
- 原来Github上的README.md文件这么有意思——Markdown语言详解
- Unity新UI系统概述——Animation Integration
- source insight使用技巧
- bzoj1007: [HNOI2008]水平可见直线 凸包