zoukankan      html  css  js  c++  java
  • Android开发中按钮的语法

    按钮的主要作用就是触发一个动作,所以会用到监听器。

    如何为按钮添加单机事件监听器:

    1.匿名内部类作为单机事件监听器

    案例:

    首先在.xml文件中添加一个按钮一,然后设置其id属性,然后在main里获取按钮id,并添加动作监听

    .xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮一"
             />
    
    </android.support.constraint.ConstraintLayout>

    MainActivity.java

    package com.example.myapplication2;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button button=findViewById(R.id.button1);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(MainActivity.this,"单机了按钮一",Toast.LENGTH_SHORT).show();
                }
            });
    
        }
    }

    2.通过onClick 属性实现

          (1)在Activity的java代码中编写一个包含View类型参数的方法。

                      

    public void myClick(View view)
    {
       //编写要执行的动作代码
    }

          (2)将android:onClick属性指定为步骤(1)中的方法名。

    android:onClick="myClick"     //无括号

    案列:

    .xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮二"
            android:onClick="myClick"
             />
    </android.support.constraint.ConstraintLayout>

    MainActivity.java

    package com.example.myapplication2;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
        public void myClick(View view){
            Toast.makeText(MainActivity.this,"单机了按钮二",Toast.LENGTH_SHORT).show();
        }
    }

  • 相关阅读:
    android笔记5——同一个Activity中Fragment的切换
    JavaScript 刚開始学习的人应知的 24 条最佳实践
    位运算符之异或的化腐朽为奇妙
    unity常见问题之20题
    汉澳sinox不受openssl心血漏洞影响并分析修复其漏洞代码
    基于canvas和Web Audio的音频播放器
    poj2595(凸包)
    HDU 1257 最少拦截系统(dp)
    【iOS开发-33】学习手动内存管理临时抛弃ARC以及retain/assign知识——iOSproject师面试必考内容
    万能狗! 程序猿屌丝独自创业之路(一)
  • 原文地址:https://www.cnblogs.com/zwx655/p/12273897.html
Copyright © 2011-2022 走看看