• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

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

武飞扬头像
大牌vae
帮助1

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>
学新通

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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfahai
系列文章
更多 icon
同类精品
更多 icon
继续加载