当前位置: 首页 > article >正文

Android复习代码1-4章


public class RudioButton extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rudio_button);

        // 找到RadioGroup和TextView的实例
        RadioGroup radioGroup = findViewById(R.id.radio);
        TextView tv = findViewById(R.id.tv);

        // 在radioGroup实例上设置监听器
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.girl) {
                    tv.setText("女生");
                } else {
                    tv.setText("男生");
                }
            }
        });
    }
}
<?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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/radio"
        >
        <RadioButton
            android:id="@+id/girl"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="女"/>
        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="男"/>
    </RadioGroup>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textSize="20sp"
        android:id="@+id/tv"/>

</LinearLayout>

<?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">
    <CheckBox
        android:id="@+id/badminton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="羽毛球"
    />
    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/basketball"
        android:text="篮球"/>
    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pingpang"
        android:text="乒乓"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/hobby"
        android:textSize="30sp"/>
</LinearLayout>
package com.example.bok;

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

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;



public class hobby extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
    private TextView hobby;
    private String hobbys ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.checkbox);
        CheckBox checkBox1 = findViewById(R.id.badminton);
        CheckBox checkBox2 =findViewById(R.id.basketball);
        CheckBox checkBox3 =findViewById(R.id.pingpang);
        checkBox1.setOnCheckedChangeListener(this);
        checkBox2.setOnCheckedChangeListener(this);
        checkBox3.setOnCheckedChangeListener(this);
        hobby=findViewById(R.id.hobby);
        hobbys = new String();
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        String motion = compoundButton.getText().toString();

        if (b)
        {
            if(!hobbys.contains(motion)){
                hobbys=hobbys+motion;
                hobby.setText(hobbys);
            }}
            else {
                if(hobbys.contains(motion)){
                    hobbys=hobbys.replace(motion,"");
                    hobby.setText(hobbys);
                }
            }
        }
    }

 AndroidManifest.xml中注册Activity启动和关闭activity

    @Override
    public void onClick(View view) {
        Intent intent =new Intent(MainActivity.this, jump.class);
        startActivity(intent);
    }

 发数据

   public void onClick(View view) {
        String acc =id.getText().toString();
        String psd =psw.getText().toString();
        Intent intent =new Intent(MainActivity.this, jump.class);
        intent.putExtra("username",acc);
        intent.putExtra("psw",psd);
        startActivity(intent);
    }

接收

public class jump extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jump);
        Button btn2 = findViewById(R.id.btn2);
        Intent intent = this.getIntent();
        String username= intent.getStringExtra("username");
        String psw = intent.getStringExtra("psw");
        TextView tv1 = findViewById(R.id.tv1);
        TextView tv2 = findViewById(R.id.tv2);
        tv1.setText(username);
        tv2.setText(psw);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}
    @Override
    public void onClick(View view) {
        String acc =id.getText().toString();
        String psd =psw.getText().toString();
        Bundle bundle = new Bundle();
        bundle.putString("username",acc);
        bundle.putString("psw",psd);
        Intent intent =new Intent(MainActivity.this, jump.class);
        intent.putExtras(bundle);
        startActivity(intent);
    }
        Button btn2 = findViewById(R.id.btn2);
        Bundle bundle = this.getIntent().getExtras();
        String username= bundle.getString("username");
        String psw = bundle.getString("psw");

public class FR extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_main,container,false);
        return v;
    }
}

 


http://www.kler.cn/a/415614.html

相关文章:

  • 一次完整的CNAS软件测试实验室内部审核流程
  • opengl 三角形
  • 鸿蒙HarmonyOS学习笔记(6)
  • HTML CSS JS基础考试题与答案
  • 未来已来?AI技术革新改变我们的生活
  • 洛谷 P2385 [USACO07FEB] Bronze Lilypad Pond B C语言 bfs
  • Codigger Desktop:多样 Look 设计,全新 Game Look 带来趣味体验
  • 数据结构——哈夫曼编码
  • 鸿蒙学习相关术语
  • 如何画出漂亮的决策树?
  • 【maven-4】IDEA 配置本地 Maven 及如何使用 Maven 创建 Java 工程
  • 自动类型推导(auto 和 decltype);右值引用和移动语义
  • mysql8.0基础-锁基础(七)
  • neo4j desktop版命令行中导入导出dump
  • Unity之一键创建自定义Package包
  • 题目 3209: 蓝桥杯2024年第十五届省赛真题-好数
  • 信息学奥赛一本通 1448:【例题1】电路维修 | 洛谷 P4667 [BalticOI 2011 Day1] Switch the Lamp On 电路维修
  • 《使用Python进行数据挖掘:理论、应用与案例研究》
  • spine 动画层 动态权重
  • brew安装mongodb和php-mongodb扩展新手教程
  • 智启未来 扬帆5G:江苏移动打造“5G + 智慧教育”典范,引领教育新风尚
  • 个人博客接入github issue风格的评论,utteranc,gitment
  • Nuxt.js 应用中的 render:response 事件钩子
  • 【Java面试题】消息队列中,如何保证消息的顺序性?
  • SQL进阶——子查询与视图
  • Prophet时间序列算法总结及python实现案例