Android学习实践:7.使用布局文件进行事件绑定
来源:互联网 发布:读地理地图软件 编辑:程序博客网 时间:2024/06/05 10:42
之前响应Button的OnClick事件,都是实现了OnClickListener接口的OnClick方法来实现,其实还有更简单的方法,在布局文件的xml中进行配置。在对应的控件中加入属性 android:onClick,其值就是响应事件的方法名:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" 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="maxwoods.demo1.MainActivity" > <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:textColor="#0000FF" android:textSize="16pt"/> <!-- 加入button1 --> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/action_openactivity" android:onClick="onClick"/> <!-- 加入子布局 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <!-- 加入button2 --> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_weight="1"android:background="#FF0000" android:text="@string/action_phoneCall" android:onClick="onClick"/> <!-- 加入button3 --> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_weight="1"android:background="#00FF00" android:text="@string/action_sendSMS" android:onClick="onClick"/></LinearLayout></LinearLayout>
现在回到MainActivity.java,去掉OnClickListener接口还有相应的setOnClickListener语句,当然这里的方法名可以不用是onClick,可以改为其它名字,不过对应的布局文件中的android:onClick中的值也要修改:
package maxwoods.demo1;import maxwoods.demo1.R;import android.app.AlertDialog;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.support.v7.app.AppCompatActivity;public class MainActivity extends AppCompatActivity{public void onClick(View v){Intent intent;Uri uri;switch(v.getId()){case R.id.button1:intent=new Intent();intent.setClass(this, MyActivity.class);startActivity(intent);break;case R.id.button2:uri=Uri.parse("tel:1008611");intent=new Intent(Intent.ACTION_CALL,uri);startActivity(intent);break;case R.id.button3: uri=Uri.parse("smsto:1008611");intent=new Intent(Intent.ACTION_SENDTO,uri);startActivity(intent);break;}}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overridepublic 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;}@Overridepublic 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;}switch(id){case R.id.action_about:StringBuilder sb=new StringBuilder();sb.append("手机型号:".concat(android.os.Build.MODEL).concat("\n"));sb.append("SDK版本:".concat(Integer.toString(android.os.Build.VERSION.SDK_INT)).concat("\n"));sb.append("系统版本:".concat(android.os.Build.VERSION.RELEASE).concat("\n"));sb.append("源码控制版本:".concat(android.os.Build.VERSION.INCREMENTAL).concat("\n"));AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle(R.string.app_name);builder.setMessage(sb.toString());builder.setPositiveButton("确定",null);builder.create().show();break;case R.id.action_exit:android.os.Process.killProcess(android.os.Process.myPid());break;}return super.onOptionsItemSelected(item);}}
0 0
- Android学习实践:7.使用布局文件进行事件绑定
- android应用开发实践一:Activity与控件布局,事件监听之学习实践
- Vuejs实践--事件绑定
- Android学习笔记--控件事件绑定方式
- Android 动态布局实践 - 使用FrameLayout
- Android学习------------xml布局文件
- Android学习--布局文件解析
- 使用sklearn进行机器学习—实践
- 【Android】使用Fragment进行Activity布局
- 使用Android databinding进行动态布局
- 使用inotify进行文件事件通知
- Android中的IOC框架,完全注解方式就可以进行UI绑定和事件绑定
- 数据库学习实践(三)--使用数据绑定
- Android学习【4】Android绑定Service进行通信
- Android中xml布局文件中使用include引入布局进行复用(解决使用include布局重叠,颜色设置无效问题)
- jquery绑定事件优化网页性能(使用on进行事件委托)
- android使用文件进行存储
- Android 布局文件中使用其他布局文件的方法
- 今天终于申请到了CSDN,我来了
- PAT(甲级)1021
- JAVA编程基础(三)
- mysql 常用命令总结
- 白话经典算法系列之七 堆与堆排序
- Android学习实践:7.使用布局文件进行事件绑定
- 使用Lambda与不使用Lambda表达式
- char *str 与char str[] 的区别
- 新电脑安装系统时提示 File:\Boot\BCD Status:0xc000000e 错误解决方案
- EventBus使用详解(一)——初步使用EventBus
- poj-3065
- Redis 缓存 + Spring 的集成示例
- VMWare安装Mac OS X
- Android中自定义弹窗提醒控件