栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

activity_main.xml




    

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("被点击");

        }
    }


}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/950942.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号