Android复习代码1-4章


java 复制代码
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 复制代码
<?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 复制代码
<?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>
java 复制代码
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

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

发数据

java 复制代码
   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);
    }

接收

java 复制代码
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();
            }
        });
    }
}
java 复制代码
    @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);
    }
java 复制代码
        Button btn2 = findViewById(R.id.btn2);
        Bundle bundle = this.getIntent().getExtras();
        String username= bundle.getString("username");
        String psw = bundle.getString("psw");

java 复制代码
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;
    }
}

相关推荐
二流小码农8 分钟前
鸿蒙开发:如何更新对象数组
android·ios·harmonyos
Billy_Zuo27 分钟前
Android Studio中创建第一个Flutter项目
android·flutter·android studio
RabbitYao40 分钟前
Google TextToSpeech apk 添加离线语音包再重新编译
android
w23617346012 小时前
Android四大核心组件
android·四大组件
Dnelic-2 小时前
移动通信行业术语
android·telephony·自学笔记
每次的天空3 小时前
Android学习总结之扩展基础篇(一)
android·java·学习
心之所向,自强不息3 小时前
关于Android Studio的Gradle各项配置2
android·gradle·android studio
EQ-雪梨蛋花汤3 小时前
【Flutter】Unity 三端封装方案:Android / iOS / Web
android·flutter·unity
foenix664 小时前
PicoVR眼镜在XR融合现实显示模式下无法显示粒子问题
android·unity·c#·xr·pico
一杯凉白开5 小时前
为了方便测试,程序每次崩溃的时候,我都让他跳转新页面,把日志显示出来
android