• Android学习(一) 按钮的事件


    用户登录

    1、内部匿名类方式实现

    layout

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context=".MainActivity" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="80dp"
            android:layout_height="35dp"
            android:textSize="16dp"
            android:gravity="bottom"
            android:text="@string/lab_loginid" />
    
        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/textView1"
            android:hint="@string/txt_hint_loginid" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="80dp"
            android:layout_height="35dp"
            android:textSize="16dp"
            android:gravity="bottom"
            android:layout_below="@+id/textView1"
            android:text="@string/lab_loginpwd" />
    
        <EditText
            android:id="@+id/editText2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/editText1"
            android:layout_toRightOf="@+id/textView2"
            android:ems="10"
            android:hint="@string/txt_hint_loginpwd"
            android:inputType="textPassword" >
        </EditText>
    
        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView2"
            android:text="@string/btn_login" />
    
        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/button1"
            android:src="@drawable/ic_btnlogin"/>
            
    </RelativeLayout>

    java代码

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // 获取页面中的按钮对象
            Button btnlogin = (Button) findViewById(R.id.button1);
    
            // 添加事件,匿名内部类方式
            btnlogin.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    String loginid = ((EditText) findViewById(R.id.editText1)).getText().toString();
                    String loginpwd = ((EditText) findViewById(R.id.editText2)).getText().toString();
    
                    if (loginid.equals("admin") && loginpwd.equals("123123")) {
                        //页面上弹出提示
                        Toast.makeText(MainActivity.this, "登录成功", 1).show();
                    } else {
                        Toast.makeText(MainActivity.this, "登录失败", 1).show();
                    }
                }
            });
        }
    }

    2、外部类方式实现

    首先定义个类,实现OnClickListener接口

    btnclickListener.java
    public class btnclickListener implements OnClickListener {
    
        @Override
        public void onClick(View arg0) {
         //当调用公共的事件时,可以采用这种方式 System.out.println(
    "onclick"); Log.e("click", "外部类点击事件"); } }

    MainActivity.java

    btnlogin.setOnClickListener(new btnclickListener(){
      @Override
        public void onClick(View arg0) {
        super.onClick(arg0);  //调用父类的事件,也就是btnclickListener的Click事件
        Toast.makeText(MainActivity.this, "登录成功", 1).show();  
      }
    });

    3、接口方式实现事件

    首先用当前Activity继承事件接口,并实现事件方法,然后设置setOnClickListener方法使用this。

    public class MainActivity extends Activity implements OnClickListener{
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // 获取页面中的按钮对象
            Button btnlogin = (Button) findViewById(R.id.button1);
     
                    //接口方式实现事件
            btnlogin.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View arg0) {
            Log.i("接口方式实现事件", "接口方式实现事件");
        }
    }
  • 相关阅读:
    基于socket.io的实时消息推送
    mysql_use_result & mysql_store_result & MYSQLI_ASYNC
    深入浅出讲解:php的socket通信
    Mysql时间存储类型优缺点?DATETIME?TIMESTAMP?INT?
    PHP垃圾回收机制引用计数器概念
    php调试函数
    Docker生产环境实践指南
    11 个 Linux 上最佳的图形化 Git 客户端
    浅谈TCP/IP网络编程中socket的行为
    highcharts 使用实例
  • 原文地址:https://www.cnblogs.com/zhengcheng/p/4350701.html
Copyright © 2020-2023  润新知