安卓常用View(一)

本篇常用的View如图中所示:

 

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#9C9A9C"
        android:text="GODVGODVGODV"
        android:textColor="#F80B0B"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/et_simple_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入电话号"
        android:inputType="phone" />

    <Button
        android:id="@+id/btn_simple_submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="提交" />

    <ImageView
        android:id="@+id/iv_simple_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:drawable/alert_dark_frame"
        android:src="@android:drawable/ic_media_play" />

    <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:id="@+id/cb_simple_basket"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="篮球" />

        <CheckBox
            android:id="@+id/cb_simple_foot"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="足球" />

        <CheckBox
            android:id="@+id/cb_simple_pingpang"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="乒乓球" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="tip"
            android:text="提示" />
    </LinearLayout>

    <RadioGroup
        android:id="@+id/rg_simple_sex"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

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

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

        <RadioButton
            android:id="@+id/rb_simple_secret"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="未知" />
    </RadioGroup>

</LinearLayout>

java代码

public class SimpleComponentActivity extends Activity {
    private EditText et_simple_phone;
    private Button btn_simple_submit;
    private ImageView iv_simple_play;
    private CheckBox cb_simple_basket;
    private CheckBox cb_simple_foot;
    private CheckBox cb_simple_pingpang;
    private RadioGroup rg_simple_sex;
    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple);
        rg_simple_sex = findViewById(R.id.rg_simple_sex);
        rg_simple_sex.setOnCheckedChangeListener((radioGroup, checkedId) -> {
            RadioButton rb = findViewById(checkedId);
            Toast.makeText(SimpleComponentActivity.this, rb.getText().toString(), Toast.LENGTH_SHORT).show();
        });
        et_simple_phone = findViewById(R.id.et_simple_phone);
        btn_simple_submit = findViewById(R.id.btn_simple_submit);
        cb_simple_basket = findViewById(R.id.cb_simple_basket);
        cb_simple_foot = findViewById(R.id.cb_simple_foot);
        cb_simple_pingpang = findViewById(R.id.cb_simple_pingpang);
        btn_simple_submit.setOnClickListener(view -> {
            String phone = et_simple_phone.getText().toString();
            Toast.makeText(SimpleComponentActivity.this, phone, Toast.LENGTH_SHORT).show();
        });
        iv_simple_play = findViewById(R.id.iv_simple_play);
        iv_simple_play.setOnClickListener(view -> {
            iv_simple_play.setBackgroundResource(android.R.drawable.alert_light_frame);
            iv_simple_play.setImageResource(android.R.drawable.ic_media_pause);
        });
        onCheckedChangeListener = (compoundButton, b) -> {
            if (b) {
                Toast.makeText(SimpleComponentActivity.this, compoundButton.getText().toString() + "被选中", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(SimpleComponentActivity.this, compoundButton.getText().toString() + "被取消", Toast.LENGTH_SHORT).show();
            }

        };
        cb_simple_foot.setOnCheckedChangeListener(onCheckedChangeListener);
        cb_simple_basket.setOnCheckedChangeListener(onCheckedChangeListener);
        cb_simple_pingpang.setOnCheckedChangeListener(onCheckedChangeListener);

    }

    public void tip(View view) {
        StringBuffer sb = new StringBuffer();
        if (cb_simple_basket.isChecked()) {
            sb.append(cb_simple_basket.getText().toString()).append(" ");
        }
        if (cb_simple_foot.isChecked()) {
            sb.append(cb_simple_foot.getText().toString()).append(" ");
        }
        if (cb_simple_pingpang.isChecked()) {
            sb.append(cb_simple_pingpang.getText().toString());
        }
        Toast.makeText(SimpleComponentActivity.this, sb.toString(), Toast.LENGTH_SHORT).show();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值