安卓基本布局组件——JAVA代码方式创建视图界面——监听器的基本用法

LinearLayout布局

必须属性:

  • layout_width
  • layout_height
    可选参数:
    • match_parent => 占满父布局的宽度
    • wrap_content => 随着内容的宽高变化

选择横向或纵向排列的属性:orientation

  • 横向:horizontal
  • 纵向:vertical

TextView组件

属性:

  • background => 可以是图片资源或者颜色等
  • layout_width
  • layout_height
  • text => 文本内容

EditView组件

属性:

  • layout_width
  • layout_height
  • hint => 提示内容,提示用户此处输入什么
  • inputType => 输入类型,可以为textPassword和number等

Radio组件(单选按钮)

需要一个RadioGroup来确定哪些按钮是一组

属性:同LinearLayout

每个组里面有RadioButton按钮

属性:

  • text => 单选框内容
  • checked => 如果为true默认选择,一个组中只有一个

CheckBox(多选按钮)

没有单选框类似的RadioGroup

其余属性类似


Button按钮

属性:类似其他组件


以上按钮在XML中写,在java代码中布局

方式为:setContentView(布局资源文件)

在java代码中获取组件的方法:findViewById(资源id)


代码例子:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户:"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:background="#008577"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入"
            android:inputType="textPassword" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="性别:"/>

        <RadioGroup
            android:id="@+id/rg_gender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <RadioButton
                android:id="@+id/rb_boy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="男"/>
            <RadioButton
                android:id="@+id/rb_girl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="女"/>
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="爱好:"/>
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="电影"/>
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="读书" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_reg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="注册" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="取消"/>
    </LinearLayout>
</LinearLayout>

java代码创建视图界面

1.创建组件(new方法)

2.设置内容等

3.布局,setContentView(组件)


注册监听器

按钮监听器:

button.setOnClickListener()

单选框监听器:

rgGender.setCheckedchangeListener()

代码例子:

package net.onest.androidch0201;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

        //获取布局文件中视图控件的对象
//        TextView tvText = findViewById(R.id.tv_hello);
//        tvText.setText("Hello Android!");


        //使用Java代码创建视图界面
        //Context环境上下文
//        TextView tvContent = new TextView(this);
//        tvContent.setText("我是使用Java代码创建的");
//        setContentView(tvContent);


        //处理按钮的点击事件
        Button btnReg = findViewById(R.id.btn_reg);
        //注册监听器
        btnReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//点击当前按钮会自动调用
                Log.e("MainActivity","点击了按钮");
            }
        });

        //获取控件的引用
        RadioGroup rgGender = findViewById(R.id.rg_gender);
        final RadioButton rbBoy = findViewById(R.id.rb_boy);
        final RadioButton rbGirl = findViewById(R.id.rb_girl);

        //注册监听器
        rgGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //选中单选按钮
                switch (checkedId){
                    case R.id.rb_boy://选中男
                        rbBoy.setChecked(true);
                        rbGirl.setChecked(false);
                        break;
                    case R.id.rb_girl://选中女
                        rbBoy.setChecked(false);
                        rbGirl.setChecked(true);
                        break;
                }

            }
        });
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值