Android Intent页面跳转实现,以及多选框的使用(点击按钮显示所选歌手名字)

1.首先看运行行界面

2.首页代码

2.1主函数代码

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivityFirst extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        Button button = findViewById(R.id.button);
        //按钮进行监听
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //监听按钮,如果点击,就跳转
                Intent intent = new Intent();
                //前一个(MainActivity.this)是目前页面,后面一个是要跳转的下一个页面
                intent.setClass(MainActivityFirst.this,MainActivityFirst2.class);
                startActivity(intent);
            }
        });
    }
}

2.2布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/打开新的Activity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100px"
        android:text="打开新的Activity" />
</LinearLayout>

3.跳转页面代码

3.1主函数代码

package com.example.experience4;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivityFirst2 extends AppCompatActivity {
    private CheckBox checkBox1,checkBox2,checkBox3,checkBox4;
    private TextView textView;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first2);
        checkBox1=(CheckBox)findViewById(R.id.checkBox1);
        checkBox2=(CheckBox)findViewById(R.id.checkBox2);
        checkBox3=(CheckBox)findViewById(R.id.checkBox3);
        checkBox4=(CheckBox)findViewById(R.id.checkBox4);
        textView=(TextView)findViewById(R.id.textView);
        button=findViewById(R.id.button);

      button.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            String text ="你喜欢的歌手:";
            if(!checkBox1.isChecked()&&!checkBox2.isChecked()&&!checkBox3.isChecked()&&!checkBox4.isChecked()){
                Toast.makeText(MainActivityFirst2.this,"请选择歌手!", Toast.LENGTH_LONG).show();
            }
            else {
                if(checkBox1.isChecked()){
                    text+= "\n"+checkBox1.getText().toString()+"\n";
                }
                if(checkBox2.isChecked()){
                    text+=checkBox2.getText().toString()+"\n";
                }
                if(checkBox3.isChecked()){
                    text+=checkBox3.getText().toString()+"\n";
                }
                if(checkBox4.isChecked()){
                    text+=checkBox4.getText().toString()+"\n";
                }
                textView.setText(text);
                text="";
            }
        }
    });
}}

3.2布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="200px"
        android:layout_height="wrap_content"
        android:layout_marginLeft="200px"
        android:layout_marginTop="100px"
        android:text="邓丽君" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="200px"
        android:layout_height="wrap_content"
        android:layout_marginLeft="200px"
        android:layout_marginTop="70px"
        android:text="周华健" />

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="200px"
        android:layout_height="wrap_content"
        android:layout_marginLeft="200px"
        android:layout_marginTop="70px"
        android:text="费玉清" />

    <CheckBox
        android:id="@+id/checkBox4"
        android:layout_width="200px"
        android:layout_height="wrap_content"
        android:layout_marginLeft="200px"
        android:layout_marginTop="70px"
        android:text="王菲" />

    <Button
        android:id="@+id/button"
        android:layout_width="300px"
        android:layout_height="wrap_content"
        android:layout_marginTop="50px"
        android:layout_marginLeft="200px"
        android:text="选择歌手" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="400px"
        android:layout_height="wrap_content"
        android:layout_marginLeft="220px"
        android:layout_marginTop="300px"
         />
</LinearLayout>

我知道肯定会有同校的人来到这里,可以给个关注嘛?

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值