重温安卓小控件练习(一)

最近刚毕业,马上要入职新公司了,方向为Android framework,开始简单复习一下吧。
希望也能给刚开始学习android的同学提供一点帮助。

在这里插入图片描述

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮一"
        app:layout_constraintBottom_toTopOf="@+id/btn_two"
        app:layout_constraintEnd_toEndOf="@+id/btn_two"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/btn_two"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.877" />

    <Button
        android:id="@+id/btn_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="24dp"
        android:onClick="click"
        android:text="按钮二"
        app:layout_constraintBottom_toTopOf="@+id/btn_three"
        app:layout_constraintEnd_toEndOf="@+id/btn_three"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="@+id/btn_three" />

    <Button
        android:id="@+id/btn_three"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="408dp"
        android:text="按钮三"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.476"
        app:layout_constraintStart_toStartOf="parent" />

    <RadioGroup
        android:id="@+id/rdg"
        android:layout_width="257dp"
        android:layout_height="75dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <RadioButton
            android:id="@+id/rbtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />

        <RadioButton
            android:id="@+id/radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="" />
    </RadioGroup>

    <TextView
        android:id="@+id/tv"
        android:layout_width="147dp"
        android:layout_height="28dp"
        android:layout_marginTop="24dp"
        android:text="您输入的性别为:"
        app:layout_constraintEnd_toEndOf="@+id/rdg"
        app:layout_constraintStart_toStartOf="@+id/rdg"
        app:layout_constraintTop_toBottomOf="@+id/rdg" />

    <CheckBox
        android:id="@+id/like_shuttlecock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="羽毛球"
        app:layout_constraintBottom_toBottomOf="@+id/rdg"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.546" />

    <CheckBox
        android:id="@+id/like_basketball"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="篮球"
        app:layout_constraintEnd_toEndOf="@+id/like_shuttlecock"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/like_shuttlecock"
        app:layout_constraintTop_toBottomOf="@+id/like_shuttlecock" />

    <CheckBox
        android:id="@+id/like_pingpang"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="乒乓球"
        app:layout_constraintEnd_toEndOf="@+id/like_basketball"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/like_basketball"
        app:layout_constraintTop_toBottomOf="@+id/like_basketball" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择爱好"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.218"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.509" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="88dp"
        android:text="您选择的爱好为:"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.785" />

    <TextView
        android:id="@+id/hobby"
        android:layout_width="107dp"
        android:layout_height="17dp"
        app:layout_constraintBottom_toBottomOf="@+id/textView3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.252"
        app:layout_constraintStart_toEndOf="@+id/textView3"
        app:layout_constraintTop_toTopOf="@+id/textView3"
        app:layout_constraintVertical_bias="1.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package cn.itcast.myapplication;

import android.app.AlertDialog;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    private Button btn_one, btn_two, btn_three;
    private RadioGroup radioGroup;
    private TextView textView;
    private TextView hobby;
    private String hobbys;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
        btn_one = (Button) findViewById(R.id.btn_one) ;
        btn_two = (Button) findViewById(R.id. btn_two) ;
        btn_three=(Button) findViewById(R.id.btn_three);
        radioGroup=(RadioGroup) findViewById(R.id.rdg);
        textView=(TextView)findViewById(R.id.tv);
        CheckBox shuttlecock=(CheckBox)findViewById(R.id.like_shuttlecock);
        CheckBox basketball=(CheckBox)findViewById(R.id.like_basketball);
        CheckBox pingpang=(CheckBox)findViewById(R.id.like_pingpang);
        shuttlecock.setOnCheckedChangeListener(this);
        basketball.setOnCheckedChangeListener(this);
        pingpang.setOnCheckedChangeListener(this);
        hobby = (TextView)findViewById(R.id.hobby);
        hobbys = new String();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId==R.id.rbtn){
                    textView.setText("你选择了性别男");
                }else {
                    textView.setText("你选择了性别女");
                }
            }
        });
        btn_three.setOnClickListener(this);
        btn_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_one.setText("按钮一已被点击");
            }
        });
    }
    public void click(View view){
        btn_two.setText("按钮二已被点击");
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        String motion = buttonView.getText().toString();
        if(isChecked) {
            if (!hobbys.contains(motion)) {
                hobbys = hobbys + motion;
                hobby.setText(hobbys);
            }
        }else {
            if (hobbys.contains(motion)){
                hobbys=hobbys.replace(motion,"");
                hobby.setText(hobbys);
            }
        }

    }

    @Override
    public void onClick(View v){
//        switch (v.getId()){
//            case R.id.btn_three:
//                btn_three.setText("被点击");
//                break;
//        }

        if (v.getId()==R.id.btn_three){
            Toast.makeText(MainActivity.this,"按钮三被点击",Toast.LENGTH_LONG).show();
            btn_three.setText("被点击");

        }
    }


}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

那天的烟花雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值