Android Button点击事件的三种方式
来源:互联网 发布:屏幕像素算法 编辑:程序博客网 时间:2024/05/17 07:03
一、在XML中
<LinearLayoutxmlns: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:orientation="vertical"<!-- 垂直分布 -->>
tools:context="${relativePackage}.${activityClass}">
<EditText
android:id="@+id/edit_text"<!-- 用于获取EditText -->>
android:layout_width="wrap_content"<!-- 包裹内容 -->>
android:layout_height="wrap_content"
android:text="@string/hello_world1"/><!-- strings.xml引入 -->>
<Button
android:id="@+id/clear_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击清除输入"/>
</LinearLayout>
//---------------------------------------------------------
对应的.java中
package com.example.myandroid;
import android.app.Activity;
import android.os.Bundle;
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;
public class MainActivityextends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText = (EditText)findViewById(R.id.edit_text) ;//获取控件
Button button = (Button)findViewById(R.id.clear_button);
button.setOnClickListener(new Button.OnClickListener(){//创建监听
public void onClick(View v) {
editText.setText("");
}
});
};
}
二、xml中无变化在.java中button.setOnClickListener(listener);
Button.OnClickListener listener = new Button.OnClickListener(){//创建监听对象
public void onClick(View v){
editText.setText("");
}
};
三、xml中按钮修改为 <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"/>
.java文件中
public void sendMessage(View view){
editText.setText("");
}
- Android Button点击事件的三种方式
- Android之Button按钮点击事件的三种方式
- Button点击事件的三种实现方式
- Android之Button点击事件处理的几种方式
- Android之Button按钮点击事件的四种方式
- Android之Button按钮点击事件的三种方法
- android 触发Button按钮事件的三种方式
- android菜鸟之button事件的三种方式
- android 监听Button点击的事件的方式
- button 事件的三种处理方式
- Android Button的点击事件
- [Android][Button]Button注册点击事件的4种方法
- Android-学习笔记之 Button实现点击事件的几种方式
- Android监听Button等控件的点击事件的三种方法
- Button 点击事件onclick实现的四种方式
- Button实现点击事件的几种方式
- Ext.Button点击事件的三种写法
- Ext.Button点击事件的三种写法及比较
- 可变数组和不可变数组
- linux top VIRT RES SHR SWAP DATA内存参数详解
- 解决 ffmpeg 在 avformat_alloc_output_context2 或者 av_guess_format 获取失败的问题
- (运存扩展器)Android手机内存扩展软件RAMEXPANDER使用教程
- 谈Objective-C 内存管理机制
- Android Button点击事件的三种方式
- COPY 推送服务设计要点
- Linux - 常用管理文件和目录的命令
- NOJ1858 对于最长上升子序列O(nlogn)算法的深入理解
- ACM/ICPC 2014 北京站题 解题报告
- php对象转成数组和数组转成对象操作
- Android开发神器(里面有各种UI特效和android代码库实例)
- nginx反向代理,动静态请求分离,以及nginx缓存应用,以及使用ngx_cache_purge清除指定URL
- iOS 图片命名规则