美文网首页
按钮事件

按钮事件

作者: 古拉啦啦 | 来源:发表于2019-09-26 19:16 被阅读0次
 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

  <Button
    android:id="@+id/Bu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Edit"
    android:background="@color/colorAccent"
    android:tag="1"
  />
<Button
android:id="@+id/bu2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="100dp"
android:enabled="false"
android:text="完成"/>
 <Button
   android:id="@+id/bu3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/bu2"
    android:layout_marginLeft="200dp"
    android:text="编辑"/>
</RelativeLayout>

main中

 package swu.wll.buttom;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

/**
 * 按钮通常能接受一个参数:view
 * 当按钮被点击,系统接受这个事件,并把这个事件回调给监听者,把  当  前这个被点击的按钮作为参数毁掉过去
*/
public class MainActivity extends AppCompatActivity implements       View.OnClickListener {
private  boolean isnormal = true;
Button button;
Button done;
Button edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button= findViewById(R.id.Bu);
    done = findViewById(R.id.bu2);
    edit = findViewById(R.id.bu3);

button.setOnClickListener(this);
done.setOnClickListener(this);
edit.setOnClickListener(this);

  }

@Override
public void onClick(View view) {
    //事件处理
    Button bt =(Button ) view;
//        if(bt.getText().equals("Edit")){
  //            bt.setText("Done");
//        }else{
//            bt.setText("Edit");
//        }

//        //判断状态
//if(isnormal){
//    bt.setText("Done");
//}else {
//    bt.setText("Edit");
//}
//isnormal = !isnormal;
 if(bt.getId()== R.id.Bu) {
     if (bt.getTag().equals("1")) {
         bt.setText("Done");
         bt.setTag("0");
     } else {
         bt.setText("Edit");
         bt.setTag("1");
     }
 }else if(bt.getId()== R.id.bu3){
     done.setEnabled(true);
     edit.setEnabled(false);
 }else {
     done.setEnabled(false);
     edit.setEnabled(true);
   }

  }

}

Button常用属性

android:drawable //放一个drawable资源
android:drawableTop //可拉伸要绘制的文本的上面
android:drawableBottom //可拉伸要绘制的文本的下面
android:drawableLeft //可拉伸要绘制的文本的左侧
android:drawableRight //可拉伸要绘制的文本的右侧
android:text //设置显示的文本
android:textColor //设置显示文本的颜色
android:textSize //设置显示文本字体大小
android:background //可拉伸使用的背景
android:onClick //设置点击事件

Button的状态

android:state_pressed //是否按下,如一个按钮触摸或者点击。
android:state_focused //是否取得焦点,比如用户选择了一个文本框。
android:state_hovered //光标是否悬停,通常与focused state相同,它是4.0的新特性
android:state_selected //被选中状态
android:state_checkable //组件是否能被check。如:RadioButton是可以被check的。
android:state_checked //被checked了,如:一个RadioButton可以被check了。
android:state_enabled //能够接受触摸或者点击事件
android:state_activated //被激活
android:state_window_focused //应用程序是否在前台,当有通知栏被拉下来或者一个对话框弹出的时候应用程序就不在前台了

Button的点击事件(常用的两种)

一、通过实现OnClickListener接口

public class MainActivity extends AppCompatActivity implements  View.OnClickListener {
//实现OnClickListener接口
@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_main);
    //找到Button,因为是返回的是VIEW,所以我们进行强转
    Button btn = (Button) findViewById(R.id.btn);
    //绑定监听
    btn.setOnClickListener(this);
}

//重写onClick()方法
@Override
public void onClick(View v) {
    Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
}
}

二、使用匿名内部类

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_main);
    Button btn = (Button) findViewById(R.id.btn);
    //使用匿名内部类
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
         }
      });
  }
}

相关文章

网友评论

      本文标题:按钮事件

      本文链接:https://www.haomeiwen.com/subject/hbduyctx.html