Android应用开发技术基础学习篇

学习Android差不多一个月了,这段时间里用Android Stidio做了几个小小的页面,想整理一下自己这段时间里对Android的学习情况,更深入了解自己对知识的掌握做个总结:

基本控件

布局管理器线性布局(LinearLayout)相对布局(RelativeLayout)
(布局有很多种,一般用得最频繁的就是这两种布局)
LinearLayout 最常用的属性:

属性名称 作用描述
android:id 控件的名称标识,可以通过这个id找到这个控件
android:layout_width 控件的宽度
android:layout_margin 控件的高度
android:layout_margin 控件的外边距
android:layout_padding 控件的内边距
android:background 控件的背景可以设置颜色 、图片
android:orientation 控件的方向,可设置横向线性布局或者纵向线性布局

以上这些属性不仅是LinearLayout的属性也是RelativeLayout的属性,包括Button、TextView都有这些Button、TextView没有android:orientation这个属性。

常用的界面控件

属性名称 作用描述
TextView 显示文本信息
Button 普通按钮
EditText 可编辑的文本框组件(输入框)
ImageView 用于显示图片
ImageButton 图片按钮
CheckBox 复选框
RadioGroup 单选按钮组
Dialog 对话框
Toast 信息提示组件

在XML中修改某个属性的值

              <TextView  android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:id="@+id/tv1"
               android:textColor="#FFFFFF"
               android:textSize="18sp"
               android:background="#0000FF" 
               android:text="@string/hello_world"/>

android:id 属性声明了TextView的ID,这个ID主要用于在代码中引用了这个TextView对象。
“@+id/tv1”表示所设置的ID值,@表示 后面的字符是ID资源,加号(+)表示需要建立新资源名称,并添加到R.java文件,斜杠后面的字符串(tv1)表示新资源的名称。
android:textColor属性设置了文本颜色, android:textSize属性设置了文本字体;
android:background属性设置了背景颜色,text属性设置了文本内容。

Button控件

Buttom的基本使用方法有以下几种

  1. 添加Buttom控件到XML布局文件中,也可通过程序添加

    在布局文件中设置按钮的一些属性,如位置、宽高、按钮上的字、颜色等,比较重要的是要给按 钮一个ID,这是按钮唯一的名称。

  2. 处理按钮的单击事件
    按钮单击有如下两种处理方法。
    2.1: 通过onClick属性设置处理单击事件的方法名,在Activity中实现这个方法。
    在XML布局文件设置Button的属性,即android:onClick=“myclick”,然后在该布局文件对应的Activity中实现该方法。

      public void myclick(View view){
             //  Do something in response to button click
       }
    

    2.2: 另一种方法是使用setOnClickListener添加监听器对象,可以写一个内部类,实现OnClickListener接口,在这个类中实现onClick方法,方法中写按钮单击时想做的全体工作。

    Button button =(button)findViewById(R.id.button_send);
    button.setOnClickListener(new View.OnClickListene(){
         
    public void onClick(View v){
         
     //  Do something in response to button click
    }
    }
    )

实例1

在这里插入图片描述

登录页面的xml的布局

<LinearLayout
    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"
    android:orientation="vertical"
    tools:context=".LoginActivity">
//显示文本信息
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#DBDBDB"
        android:gravity="center"
        android:textSize="22sp"
        android:text="登录界面" />
//输入框
    <EditText
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名" />
//输入框
    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码" />
//登录按钮
    <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="20sp"
        android:text="登录" />

</LinearLayout>

代码部分
//1.获取用户名密码按钮

private EditText etUsername;
private EditText etPassword;
private Button btnLogin;

//2.定义变量名

etUsername=findViewById(R.id.username);
etPassword=findViewById(R.id.password);
btnLogin=findViewById(R.id.login);

//3.监听按钮的点击事件

btnLogin.setOnClickListener(this);
private void login() {
   
    String username=etUsername.getText().toString().trim();
    String password= etPassword.getText().toString();
    //3.2比较用户名和密码是否正确
    if("Meng".equals(username)&&"123456".equals(password)){
   
        Toast.
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值