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>
我知道肯定会有同校的人来到这里,可以给个关注嘛?
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgfahai
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01