【Android笔记】控件

1.新建项目
2.添加按钮(添加在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"
    android:padding="8dp">
    <Button
        android:id="@+id/btn_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮1"/>
    <Button
        android:id="@+id/btn_two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="按钮2"/>
    <Button
        android:id="@+id/btn_three"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮3"/>
</LinearLayout>

3.为控件添加功能(添加在MainActivity.java)

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button btn_one,btn_two,btn_three;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_one = (Button) findViewById(R.id.btn_one);
        btn_two  = (Button) findViewById(R.id.btn_two );
        btn_three = (Button) findViewById(R.id.btn_three);
        btn_three.setOnClickListener(this);
        btn_one.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                btn_one.setText("按钮1已被点击");
            }
        });

    }
    public void click(View view){
        btn_two.setText("按钮2已被点击");
    }
    @Override
    public void onClick(View v){
        switch(v.getId()){
            case R.id.btn_three:
                btn_three.setText("按钮3已被点击");
                break;
        }

    }

}

常用控件
TextView
EditText
Button
ImageButton
ToggleButton
CheckBox
RadioButton
ImageView

RadioButton的使用
RadioGroup为按钮组,使用方法如下

1.添加按钮

<?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">

    <RadioGroup
        android:id = "@+id/rdg"
        android:layout_width= "match_parent"
        android:layout_height= "wrap_content"
        android:orientation="vertical">
        <RadioButton
            android:id = "@+id/rbtn"
            android:layout_width= "wrap_content"
            android:layout_height= "wrap_content"
            android:textSize = "25dp"
            android:text=""/>
        <RadioButton
            android:layout_width= "wrap_content"
            android:layout_height= "wrap_content"
            android:textSize = "25dp"
            android:text=""/>
    </RadioGroup>
    <TextView
        android:id = "@+id/tv"
        android:layout_width= "wrap_content"
        android:layout_height= "wrap_content"
        android:textSize = "30dp"/>
    <!-- 后面先不用管 -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="姓名:"
        android:textSize="28sp"
        android:textColor="#000000"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入姓名"
        android:maxLines="2"
        android:textColor="#000000"
        android:textSize="20sp"
        android:textStyle="italic"/>
</LinearLayout>

2.为控件添加功能

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private RadioGroup radioGroup;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup=(RadioGroup)findViewById(R.id.rdg);
        textView=(TextView) findViewById(R.id.tv);
        radioGroup.setOnCheckedChangeListener(new
          RadioGroup.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(RadioGroup group, int checkedId) {
                  if (checkedId == R.id.rbtn) {
                      textView.setText("您的性别是:男");
                  } else {
                      textView.setText("您的性别是:女");
                  }
              }
          });

    }
}

Check的使用

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择兴趣爱好:"
        android:textColor="#FF8000"
        android:textSize="18sp"/>
    <CheckBox
        android:id="@+id/like_shuttlecock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="羽毛球"
        android:textSize="18sp"/>
    <CheckBox
        android:id="@+id/like_basketball"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="篮球"
        android:textSize="18sp"/>
    <CheckBox
        android:id="@+id/like_pingpong"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="乒乓球"
        android:textSize="18sp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="您选择的兴趣爱好为:"
        android:textColor="#FF8000"
        android:textSize="22sp"/>
    <TextView
        android:id="@+id/like_hobby"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"/>
</LinearLayout>
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements
CompoundButton.OnCheckedChangeListener {
    private TextView hobby;
    private String hobbys;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CheckBox shuttlecock = (CheckBox) findViewById(R.id.like_shuttlecock);
        CheckBox basketball = (CheckBox) findViewById(R.id.like_basketball);
        CheckBox pingpong = (CheckBox) findViewById(R.id.like_pingpong);
        shuttlecock.setOnCheckedChangeListener(this);
        basketball.setOnCheckedChangeListener(this);
        pingpong.setOnCheckedChangeListener(this);
        hobby = (TextView) findViewById(R.id.like_hobby);
        hobbys = new String();
    }

    @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);
            }
        }
    }
}

IBM计算器

界面代码

<?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:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="身高"/>
    <EditText
        android:id="@+id/shengao"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10">
        <requestFocus/>
    </EditText>
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="体重"/>
    <EditText
        android:id="@+id/tizhong"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"/>
    <Button
        android:id="@+id/jisuan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="计算"/>
    <Button
        android:id="@+id/quxiao"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="取消"/>

</LinearLayout>

功能实现

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    EditText shengao, tizhong;
    Button jisuan,quxiao;
    double height = 0;
    double weight = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        shengao=(EditText) findViewById(R.id.shengao);
        tizhong=(EditText) findViewById(R.id.tizhong);
        jisuan=(Button) findViewById(R.id.jisuan);
        quxiao=(Button) findViewById(R.id.quxiao);
        jisuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if ((shengao.getText() == null)||"".equals(shengao.getText().toString().trim())) {
                        shengao.setError("身高不能为空");
                        return;
                    }
                    if ((tizhong.getText() == null)||"".equals(tizhong.getText().toString().trim())) {
                        tizhong.setError("体重不能为空");
                        return;
                    }
                    if( (shengao.getText() != null)&& !"".equals(shengao.getText().toString().trim()) ){
                       height=Double.parseDouble(shengao.getText().toString());
                    }
                    if ((tizhong.getText() != null)&& !"".equals(tizhong.getText().toString().trim()) ){
                        weight=Double.parseDouble(tizhong.getText().toString());
                    }
                    double bmi = weight*10000/height/height;
                    if (bmi <18){
                        Toast.makeText(MainActivity.this,"您的身材偏瘦,请加强营养",Toast.LENGTH_SHORT).show();
                    }else if (bmi <25){
                        Toast.makeText(MainActivity.this,"您的身材标准,请继续保持",Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(MainActivity.this,"您的身材偏胖,请加强锻炼",Toast.LENGTH_SHORT).show();
                    }
                }catch (NumberFormatException e){
                    Toast.makeText(MainActivity.this,"身材或体重必须是数字",Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }


        });
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shengao.setText("");
                tizhong.setText("");
            }
        });

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值