Android 布局

Android四种监听机制(匿名内部类监听、布局监听、本类监听、布局监听)。

Activity部分:

<span style="font-size:18px;">//四种事件监听机制
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity
        implements View.OnClickListener{
    private  Button login,rgdt,submit,exit;
    private EditText user,pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        login = (Button)this.findViewById(R.id.bt1);//匿名内部类监听
        rgdt = (Button) this.findViewById(R.id.bt2);//本类监听
        submit = (Button) findViewById(R.id.submit);//本类监听
        user = (EditText) findViewById(R.id.et_user);//布局监听
        pwd = (EditText) findViewById(R.id.et_pwd);//布局监听
        exit = (Button) findViewById(R.id.bt4);//内部类监听

       /*本类监听需要实现View.OnClickListener接口,
        再设置setOnClickListener(this).
         */
        rgdt.setOnClickListener(this);
        submit.setOnClickListener(this);

        /*内部类监听需要实现View.OnClickListener接口,
        再设置setOnClickListener(new ...).
         */
        exit.setOnClickListener(new Bt4_Onclick());


        //匿名内部类监听
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("====","debug");

        //跳转画面字符串相等
             if (user.getText().toString().equals("123")
                     &&pwd.getText().toString().equals("123")){

                 Intent intent = new Intent(LoginActivity.this,RelativeActivity.class);
                 startActivity(intent);
             } else {
                     Toast.makeText(getBaseContext(),
                             "您点击了登录按钮",
                             Toast.LENGTH_SHORT).show();
             }

            }
        });
    }



    //本类监听
    @Override
    public void onClick(View v) {
        if (v.getId() ==  R.id.bt2){
            Toast.makeText(getBaseContext(),
                    "您点击了注册按钮",
                    Toast.LENGTH_SHORT).show();
        } else if (v.getId() == R.id.submit){
            Toast.makeText(getBaseContext(),
                    "您点击了提交按钮",
                    Toast.LENGTH_SHORT).show();
        }
    }

    //布局监听,注意:: 方法的参数必须是View.
    public void reset(View v){
        user.setText("");
        pwd.setText("");

      Toast.makeText(getBaseContext(),
                    "您点击了重置按钮",
              Toast.LENGTH_SHORT).show();
    }

    //内部类监听,
    public class Bt4_Onclick implements View.OnClickListener{
        public void onClick(View v) {
            finish();
        }
    }

}</span>

xml部分:

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    tools:context="com.jerehedu.jerehch01.LoginActivity">
<TextView
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:id="@+id/tv"
    android:textSize="20sp"
    android:textColor="#620a7b"
    android:text="基本控件复习"
    android:background="@color/textViewColor"
    android:gravity="center"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/et_user"
    android:hint="@string/hint_user"
    android:drawableLeft="@mipmap/ic_launcher"
    android:layout_below="@+id/tv"
    />
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/et_pwd"
    android:hint="@string/hint_pwd"
    android:drawableLeft="@mipmap/ic_launcher"
    android:inputType="textPassword"
    android:layout_below="@id/et_user"
    android:imeOptions="actionGo"/>
<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_below="@+id/et_pwd"
    android:id="@+id/rg">
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="吃" />
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="喝"/>
</RadioGroup>
<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/cb1"
    android:text="记住密码"
    android:layout_below="@+id/rg"/>
 <CheckBox
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/cb2"
     android:text="自动登录"
     android:layout_below="@+id/rg"
     android:layout_toRightOf="@id/cb1"/>
 <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_below="@id/cb2"
     android:orientation="horizontal"
     android:gravity="center"
     android:id="@+id/ll">
     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="登录"
         android:id="@+id/bt1"
         android:layout_below="@+id/cb2"/>
     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="注册"
         android:id="@+id/bt2"
         android:layout_below="@+id/cb2"
         android:layout_toRightOf="@id/bt1"/>
     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="重置"
         android:id="@+id/bt3"
         android:layout_below="@+id/cb2"
         android:layout_toRightOf="@id/bt2"
         android:onClick="reset"/>
     <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="退出"
         android:id="@+id/bt4"
         android:layout_below="@+id/cb2"
         android:layout_toRightOf="@id/bt3"
         android:onClick="reset"/>
 </LinearLayout>
<ImageView
    android:layout_width="400dp"
    android:layout_height="150dp"
    android:src="@mipmap/car"
    android:layout_below="@+id/ll"
    android:layout_centerHorizontal="true"
    android:id="@+id/iv"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="提交"
    android:layout_below="@+id/iv"
    android:layout_centerHorizontal="true"
    android:id="@+id/submit"/>

</RelativeLayout></span>
界面:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值