Android开发学习笔记整理(3)

EditText

主要作用:输入框

常见属性

  • 同TextView
  • 其他常用属性:hint、maxLength、inputType

inputType的常见取值

  • text:文本
  • textPassword:文本密码框
  • numberPassword:数字密码
  • numberDecimal:带小数的数字
  • number:整型数字

ImageView

主要作用:用于显示图片

常见属性

  • 同View
  • 其他常用属性:src、scaleType (拉伸类型)

scaleType的常见取值

  • fitXY:拉伸图片填充视图
  • fitStart:保持宽高比,使图片位于上方或左侧
  • fitCenter:保持宽高比,使图片位于中间
  • fitEnd:保持宽高比,使图片位于下方或右侧
  • center:保持原尺寸,使图片位于中间
  • centerCrop:拉伸图片填充视图,使图片位于中间
  • fitCenter:保持宽高比,拉伸图片位于中间
  • centerInside:保持宽高比,缩小图片位于中间

ImageButton

主要作用:显示图片的按钮

常见属性

  • 同ImageView

响应事件

  • 获取按钮
  • 添加事件
  • 执行业务逻辑

代码部分:

part1:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/editViewButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:layout_gravity="center_horizontal"
        android:text="EditView"
        android:textSize="30sp"
        android:textAllCaps="false">
    </Button>

    <Button
        android:id="@+id/imageViewButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:layout_gravity="center_horizontal"
        android:text="ImageView"
        android:textSize="30sp"
        android:textAllCaps="false">
    </Button>

    <Button
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:layout_gravity="center_horizontal"
        android:text="ImageButton"
        android:textSize="30sp"
        android:textAllCaps="false">
    </Button>

</LinearLayout>
效果图:

在这里插入图片描述

activity_edit_text.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="演示EditText"
        android:textSize="30dp"
        android:gravity="center"
        android:textColor="#000000">
    </TextView>

    <!--
        text和hint的区别:hint是提示语,text是输入框的内容
    -->
    <EditText
        android:id="@+id/editText1"
        android:layout_margin="15dp"
        android:layout_height="50dp"
        android:layout_width="match_parent"
        android:hint="请输入用户名"
        android:maxLength="20"
        android:inputType="text">
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_margin="15dp"
        android:layout_height="50dp"
        android:layout_width="match_parent"
        android:hint="请输入密码"
        android:maxLength="20"
        android:inputType="textPassword">
    </EditText>

    <EditText
        android:id="@+id/editText3"
        android:layout_margin="15dp"
        android:layout_height="50dp"
        android:layout_width="match_parent"
        android:hint="请输入数字密码"
        android:maxLength="6"
        android:inputType="numberPassword">
    </EditText>

    <EditText
        android:id="@+id/editText4"
        android:layout_margin="15dp"
        android:layout_height="50dp"
        android:layout_width="match_parent"
        android:hint="请输入整数"
        android:maxLength="6"
        android:inputType="number">
    </EditText>

    <EditText
        android:id="@+id/editText5"
        android:layout_margin="15dp"
        android:layout_height="50dp"
        android:layout_width="match_parent"
        android:hint="请输入小数"
        android:maxLength="6"
        android:inputType="numberDecimal">
    </EditText>

    <Button
        android:id="@+id/inputButton"
        android:layout_margin="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="获取输入框的内容">
    </Button>

</LinearLayout>
效果图:

在这里插入图片描述

activity_image_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ImageView演示"
        android:textSize="30sp"
        android:textColor="#000000"
        android:gravity="center_horizontal"
        android:background="#8BC34A">
    </TextView>


    <!--
        scaleType:
            fit:
                fitXY:拉伸图片填充视图,可能会失真
                fitStart:宽高等比例拉伸,并放在视图左侧
                fitCenter:宽高等比例拉伸,并放在视图中间
                fitEnd:宽高等比例拉伸,并放在视图右侧
            center:
                center:保持原尺寸,放到视图中间
                centerCrop:填满整个屏幕,并放到中间,可能会失真
                fitCenter:等比例拉伸,放到视图中间
                centerInside:
                    当图片比较小时的时候,不做处理,放到视图中间,相当于center;
                    当图片比较大时的时候,做等比例缩小处理,然后放到视图中间
    -->
    <!-- 注:图片名称不能为数字且单词首字母不能为大写,.png结尾 -->
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:src="@drawable/icon2"
        android:scaleType="centerInside"
        android:background="#AF1B1B">
    </ImageView>
    
</LinearLayout>
效果图:

在这里插入图片描述

icon2.png

在这里插入图片描述

activity_image_button.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="ImageButton"
        android:textSize="30dp"
        android:textColor="#000000"
        android:gravity="center">
    </TextView>

    <ImageButton
        android:id="@+id/imageButtonDemo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon">
    </ImageButton>

</LinearLayout>
效果图:

在这里插入图片描述

icon.png

在这里插入图片描述

part2:

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {

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

        //点击跳转editView演示界面
        findViewById(R.id.editViewButton).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, EditTextActivity.class);
                startActivity(intent);
            }

        });

        //点击跳转ImageView演示界面
        findViewById(R.id.imageViewButton).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ImageViewActivity.class);
                startActivity(intent);
            }

        });

        //点击跳转ImageButton演示界面
        findViewById(R.id.imageButton).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ImageButtonActivity.class);
                startActivity(intent);
            }

        });
    }

}

EditTextActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class EditTextActivity extends AppCompatActivity {

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

        findViewById(R.id.inputButton).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //获取输入框的值并输出
                EditText editText1 = findViewById(R.id.editText1);
                System.out.println("editText1的值是:" + editText1.getText());

                EditText editText2 = findViewById(R.id.editText2);
                System.out.println("editText2的值是:" + editText2.getText());

                EditText editText3 = findViewById(R.id.editText3);
                System.out.println("editText3的值是:" + editText3.getText());

                EditText editText4 = findViewById(R.id.editText4);
                System.out.println("editText4的值是:" + editText4.getText());

                EditText editText5 = findViewById(R.id.editText5);
                System.out.println("editText5的值是:" + editText5.getText());
            }

        });
    }

}
效果图:

在这里插入图片描述
在这里插入图片描述

ImageViewActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class ImageViewActivity extends AppCompatActivity {

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

ImageButtonActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class ImageButtonActivity extends AppCompatActivity {

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

        //给ImageButton绑定事件
        findViewById(R.id.imageButtonDemo).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                System.out.println("图片按钮被点击了...");
            }

        });

    }

}
效果图:

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值