智能移动开发

智能移动开发

单选多选对话框

OnClickListener接口
V表示事件源

java:

定义变量
初始化变量(对应)
接口

1.layout 线性布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"

    tools:context=".MainActivity">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="姓名:" />

    <EditText
        android:id="@+id/edt_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="性别:" />

    <RadioGroup
        android:id="@+id/rdg_setGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

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

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

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="爱好" />

    <CheckBox
        android:id="@+id/chk_BuleBall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="运动" />

    <CheckBox
        android:id="@+id/chk_FootBall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="学习智能移动开发" />

    <TextView
        android:id="@+id/txt_Show"
        android:layout_width="match_parent"
        android:layout_height="37dp" />

    <Button
        android:id="@+id/btn_Confirm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="确定" />

    <Button
        android:id="@+id/btn_Cancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="取消" />

</LinearLayout>

2.字体颜色(styles.xml样式)

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textSize">25dp</item>
        <item name="android:textColor">#3F51B5</item>
    </style>

</resources>

在这里插入图片描述

3.java代码

package com.example.guoapplication;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.icu.text.LocaleDisplayNames;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener, CompoundButton.OnCheckedChangeListener {
    //1.定义变量
    private EditText edtName;
    private RadioGroup rdgSexGroup;
    private RadioButton rdbMan,rdbWoman;
    private CheckBox chkBastBall,chkFootBall;
    private Button btnConfirm,btnCancel;
    private TextView txtShow;
    private String hobbys ,sex;  //定义全局字符串 爱好,性别

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //2.0初始化变量
        initView();
        btnConfirm.setOnClickListener(this);
        rdgSexGroup.setOnCheckedChangeListener(this);   //性别
        chkFootBall.setOnCheckedChangeListener(this);   //爱好1
        chkBastBall.setOnCheckedChangeListener(this); //爱好2
    }
    //2.1初始化对应
    private void initView(){
        edtName = findViewById(R.id.edt_name);
        rdgSexGroup = findViewById(R.id.rdg_setGroup);
        rdbMan = findViewById(R.id.rdb_Man);
        rdbWoman = findViewById(R.id.rdb_woman);
        chkBastBall = findViewById(R.id.chk_BuleBall);
        chkFootBall = findViewById(R.id.chk_FootBall);
        btnConfirm = findViewById(R.id.btn_Confirm);
        btnCancel =findViewById(R.id.btn_Cancel);
        txtShow = findViewById(R.id.txt_Show);

    }


    @Override
    public void onClick(View v) {
        if(R.id.btn_Confirm==v.getId())   //监听“确定”按钮点击事件
        {
            //String s = edtName.getText().toString();//创建一个局部字符串存储姓名
            //txtShow.setText(s+sex+hobbys); //显示在txtShow文本框中
            //提示信息的显示(上下文,显示内容,显示时间)
            //Toast.makeText(this,s+sex+hobbys,Toast.LENGTH_LONG).show();

            //普通对话框
            AlertDialog dialog;  //对象 
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("我的对话框");
            builder.setMessage(edtName.getText().toString()+sex+hobbys);
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    txtShow.setText(edtName.getText().toString()+sex+hobbys); //显示在txtShow文本框中
                }
            });
            dialog = builder.create();
            dialog.show();
        }
        if(R.id.btn_Cancel== v.getId())
        {
            txtShow.setText("");
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        sex = "";
        if(checkedId == R.id.rdb_Man)   //rdbMan.isChedked()
        {
            sex = "男";
            //txtShow.setText("男");
        }
        if(checkedId ==R.id.rdb_woman)
        {
            sex = "女";
            //txtShow.setText("女");
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        hobbys ="";
        if(chkBastBall.isChecked())
        {
            hobbys = hobbys+ "运动";
            //txtShow.setText(hobbys); //直接显示在txtShow
        }
        if(chkFootBall.isChecked())
        {
            hobbys = hobbys +"学习智能移动开发";
            //txtShow.setText(hobbys);
        }
        //txtShow.setText(hobbys);
    }
}


在这里插入图片描述

在这里插入图片描述

4.引导

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.guoapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity" android:theme="@style/MyTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

848698119

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

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

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

打赏作者

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

抵扣说明:

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

余额充值