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. CI调试

  2. Recover damage pictures to see the crime scene

    Few people know that when you take photos there is also a thumbnail embeded inside the file, even so ...

  3. hdu1171

    use fmax().-------TLE #define mmax(a,b) (a)>(b)?(a):(b);   ---796ms that's it! #include <stdio ...

  4. 打包新版本上传到AppStore时报错 ERROR ITMS-90034:

    今天打包新版本上传到AppStore时报错 ERROR ITMS-90034:"Missing or invalid signature.The bundle'com.xxx.xxx' at ...

  5. VMware下安装的Mac OS X如何修改显示分辨率

    VMware下安装的Mac OS X如何修改显示分辨率   我在Win7下利用VMware安装了苹果的Mac OS,安装成功启动后,发现分辨率为1920*1080,而宿机的分辨率是1366*768,我 ...

  6. MySQL 5.7.9 免安装配置

    MySQL 5.7.9 免安装配置 环境:win10 64位 mysql版本:mysql-5.7.9-winx64.zip (http://dev.mysql.com/downloads/mysql/ ...

  7. mysql 5.7压缩包安装笔记

    转载请注明出处http://www.cnblogs.com/havedream/p/5075263.html 重装系统之后准备安装mysql,看到官网上有mysql 5.7.10可以下载就点了,然后就 ...

  8. 将List<T>转化成 DataTable--调整可空类型的转化错误

    加载表结构并保持成XML string cmdText = @"select * from kb_lable_temp where 1=2"; using (SqlConnecti ...

  9. 多屏判断css改写

    function replaceBodyClass(){ for(var i in map){ if(map[i](width)){ document.body.className = documen ...

  10. 操作MySQL数据库

    向表中插入数据 insert 语句可以用来将一行或多行数据插到数据库表中, 使用的一般形式如下: insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, ...