跳转实现,以及多选框的使用(点击按钮显示所选歌手名字)"/>
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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_first);Button button = findViewById(R.id.button);//按钮进行监听button.setOnClickListener(new View.OnClickListener() {@Overridepublic 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=""android:id="@+id/打开新的Activity"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid: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;@Overrideprotected 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(){@Overridepublic 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=""android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><CheckBoxandroid:id="@+id/checkBox1"android:layout_width="200px"android:layout_height="wrap_content"android:layout_marginLeft="200px"android:layout_marginTop="100px"android:text="邓丽君" /><CheckBoxandroid:id="@+id/checkBox2"android:layout_width="200px"android:layout_height="wrap_content"android:layout_marginLeft="200px"android:layout_marginTop="70px"android:text="周华健" /><CheckBoxandroid:id="@+id/checkBox3"android:layout_width="200px"android:layout_height="wrap_content"android:layout_marginLeft="200px"android:layout_marginTop="70px"android:text="费玉清" /><CheckBoxandroid:id="@+id/checkBox4"android:layout_width="200px"android:layout_height="wrap_content"android:layout_marginLeft="200px"android:layout_marginTop="70px"android:text="王菲" /><Buttonandroid:id="@+id/button"android:layout_width="300px"android:layout_height="wrap_content"android:layout_marginTop="50px"android:layout_marginLeft="200px"android:text="选择歌手" /><TextViewandroid:id="@+id/textView"android:layout_width="400px"android:layout_height="wrap_content"android:layout_marginLeft="220px"android:layout_marginTop="300px"/> </LinearLayout>
我知道肯定会有同校的人来到这里,可以给个关注嘛?
更多推荐
Android Intent页面跳转实现,以及多选框的使用(点击按钮显示所选歌手名字)
发布评论