Android实现Button事件的处理

开发工具:Andorid Studio 1.3

运行环境:Android 4.4 KitKat

代码实现

首先是最基本的线性布局,给每个控件设立id值,以供代码中findViewById使用

<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical" tools:context=".LoginActivity"
android:id="@+id/LinLayoutMain">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editUsername"
android:hint="@string/hint_username"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editPassword"
android:hint="@string/hint_password"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imaBtnSicily"
android:src="@mipmap/state1"
android:background="@color/backgroundWhit"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/reset"
android:text="@string/reset"/>
</LinearLayout>

在界面创建的时候即onCreate的时候,绑定控件到全局变量中

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login); linLayoutMain = (LinearLayout)this.findViewById(R.id.LinLayoutMain); edtUsername = (EditText)this.findViewById(R.id.editUsername);
edtPassword = (EditText)this.findViewById(R.id.editPassword); imaBtnSicily = (ImageButton)this.findViewById(R.id.imaBtnSicily);
imaBtnSicily.setOnClickListener(SicilyListener); btnreset = (Button)this.findViewById(R.id.reset);
btnreset.setOnClickListener(ResetListener); DynamicTextViewCount = 0;
imaBtnSicily.setOnLongClickListener(SicilyLongClickListener);
}

点击图片登陆的时候,用字符串getText().toString()函数得到用户名和密码,然后进一步判断,requestFocus()函数可以让焦点停留在想要的控件中,setImageDrawable()函数可以修改ImageButton中的图片,setVisibility()函数可以设置控件是否可见

View.OnClickListener SicilyListener = new View.OnClickListener(){
@Override
public void onClick(View v){
String username = edtUsername.getText().toString();
String password = edtPassword.getText().toString();
if(username.equals("LeiBusi") && password.equals("Halo3Q")){
imaBtnSicily.setImageDrawable(getResources().getDrawable(R.mipmap.state2));
edtUsername.setVisibility(View.GONE);
edtPassword.setVisibility(View.GONE);
}
else{
imaBtnSicily.setImageDrawable(getResources().getDrawable(R.mipmap.state1));
edtPassword.setText(null);
edtPassword.setHint(R.string.error_pawword);
edtPassword.requestFocus();
}
}
};

长按图片的时候addview()函数可以动态添加控件,在这里我给每个TextView控件进行了一个编号,通过全局变量DynamicTextViewCount来计数。显示效果如上图所示

View.OnLongClickListener SicilyLongClickListener = new View.OnLongClickListener(){
@Override
public boolean onLongClick(View v){
try{
TextView DynamicTextViewTemp = new TextView(LoginActivity.this);
DynamicTextViewTemp.setText("这是动态添加的textView" + " " + Integer.toString(DynamicTextViewCount++));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT); linLayoutMain.addView(DynamicTextViewTemp, layoutParams);
Log.d("Hint", "Enter the try block");
}
catch (Exception e){
Log.d("Hint", "Can't not add TextView by count");
return false;
}
return true;
}
};

清除按钮的功能是短按重置所有内容。这里主要在于对多个或0个TextView控件的删除功能,通过getChildCount()函数获知线性布局中有多少个子元素,通过removeViewAt()函数清除特定的控件

View.OnClickListener ResetListener = new View.OnClickListener(){
@Override
public void onClick(View v){
int childCount = linLayoutMain.getChildCount();
while(DynamicTextViewCount > 0){
linLayoutMain.removeViewAt(childCount - 1);
childCount = linLayoutMain.getChildCount();
DynamicTextViewCount--;
} imaBtnSicily.setImageDrawable(getResources().getDrawable(R.mipmap.state1)); edtUsername.setText(null);
edtUsername.setHint(R.string.hint_username);
edtUsername.setVisibility(View.VISIBLE); edtPassword.setText(null);
edtPassword.setHint(R.string.hint_password);
edtPassword.setVisibility(View.VISIBLE); edtUsername.requestFocus();
}
};

效果图



一些总结

  1. 字符串匹配相等的问题:在Java语言中,String类型没有对==这个操作符进行重载,但有特定的函数equal函数进行判断是否相等。
  2. 用户名密码登陆不正确:R.string.right_username这个的实质是一个Int类型的常量,并不是String类型的常量。
  3. 动态添加按钮只有一个:addView()函数中的控件必须是没被添加过的,因此哪怕是添加相同的按钮,也必须是在局部变量中new出来的一个新控件,函数结束后会被Java回收机制回收,因此每次new出来的都是没被添加过的。

工程下载

传送门:下载

Android实现Button事件的处理的更多相关文章

  1. 界面跳转+Android Studio Button事件的三种方式

    今天学习界面跳转 java类总是不能新建成功 看了网上教程 (20条消息) 关于android studio无法创建类或者接口问题的解决方法_qq_39916160的博客-CSDN博客 可以新建了 但 ...

  2. Android 添加Button事件

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentV ...

  3. Android中Button的五种监听事件

    简单聊一下Android中Button的五种监听事件: 1.在布局文件中为button添加onClick属性,Activity实现其方法2.匿名内部类作为事件监听器类3.内部类作为监听器4.Activ ...

  4. Xamarin for android:为button设置click事件的几种方法

    原文:Xamarin for android:为button设置click事件的几种方法 在Xamarin中一个最基础的事情,就是为一个button指定click事件处理方法,可是即使是这么一件事也有 ...

  5. Android开发之Button事件实现方法的总结

    下面介绍Button事件实现的两种方法 main.xml <?xml version="1.0" encoding="utf-8"?> <Li ...

  6. Android 触发Button按钮事件的三种方式

    1.新创建一个类 2.使用内部类 3.当多个button按钮时,为简化代码而创建的实例listener 贴代码: MainActivity.Java  文件: package com.android. ...

  7. Android点击事件(click button)的四种写法

    在学习android开发和测试的时候发现不同的人对于click事件的写法是不一样的,上网查了一下,发现有四种写法,于是想比较一下四种方法的不同 第一种方法:匿名内部类 代码: package com. ...

  8. Android studio button 按钮 四种绑定事件的方法

    package com.geli_2.sujie.sujiegeili2testbutton; import android.os.Bundle; import android.support.v7. ...

  9. Android笔记之——事件的发生(2)

    Java: package com.example.helloworld;import android.os.Bundle;import android.view.KeyEvent;import an ...

随机推荐

  1. __main__:1: Warning: Unknown table 'employ' 0L

    __main__:1: Warning: Unknown table 'employ' 0L from warnings import filterwarnings import MySQLdb fi ...

  2. CSS强制文本在一行内显示若有多余字符则使用省略号表示

    这篇文章主要介绍了强制文本在一行内显示,多余字符使用省略号,设置或检索是否使用一个省略标记(...)标示对象内文本的溢出.对应的脚本特性为textOverflow 设置或检索是否使用一个省略标记(.. ...

  3. Let's see if we could reocver Line 5.3 and above deleted chat messages or not

    Forensic is a strict science and we should let the evidence speak for itself. Several months ago I s ...

  4. 一个简单的SpringMVC3 程序

    初学者对于Spring框架的难度:引用Jar包不全,或者不正确: 1.运行界面 2.客户端页面 index.jsp 的代码 <%@ page language="java" ...

  5. C 小复习

    C语言 signed 与 unsigned: C语言中,当表达式中存在有符号类型和无符号类型时所有的操作数都自动转换为无符号类型 signed ; unsigned ; cout << a ...

  6. CSS3实现轮播图效果

    CSS3实现轮播图主要是由css:background-position和css3:animation实现.且实现此轮播需要一张四个图横着相连的图片. 注(Internet Explorer 10.F ...

  7. 如何在Word表格中的某一栏添加背景颜色

     如何在Word表格中的某一栏添加背景颜色 编写人:CC阿爸 2014-3-14 用鼠标选中某一个单元格然后右键单击 下拉菜单选择.<边框和低纹>然后点<低纹>选项卡 选中色卡 ...

  8. php异常处理示例

    php异常处理使用示例,代码说明了普通错误和致命错误捕获及处理的方法.  代码如下: <?php //禁止错误输出 error_reporting(0); //设置错误处理器 set_error ...

  9. ContactsContract.CommonDataKinds【Translated By KillerLegend】

    http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.html interf ...

  10. PowerShell 方式部署Sharepoint Solution

    覆盖 Uninstall-SPSolution –Identity Caesarstone.GlobalSite.WebSite.wsp –WebApplication http://myserver ...