1,编写activity_main.xml 代码
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" tools:context=".MainActivity"> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="选择您的生肖:" androID:layout_margintop="15dp" androID:textSize="50sp" /> <Spinner androID:ID="@+ID/sp" androID:layout_wIDth="350dp" androID:layout_height="50dp" androID:layout_gravity="center_horizontal" androID:layout_margintop="30dp" /> <button androID:ID="@+ID/enter" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center_horizontal" androID:layout_margintop="50dp" androID:text="确定" /></linearLayout>
2,编写 activity_second.xml 代码
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" tools:context=".SecondActivity"> <ImageVIEw androID:ID="@+ID/img" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center_horizontal" androID:layout_margintop="40dp" /> <TextVIEw androID:ID="@+ID/text" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center_horizontal" androID:textSize="30sp" androID:layout_margintop="20dp" /> <button androID:ID="@+ID/btn" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center_horizontal" androID:layout_margintop="300dp" androID:layout_marginleft="100dp" androID:text="返回"/></linearLayout>
3,导入图片
4,编写 MainActivity 类
package com.example.chinesezodiac;import androIDx.appcompat.app.AppCompatActivity;import androID.content.Intent;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.ArrayAdapter;import androID.Widget.button;import androID.Widget.Spinner;public class MainActivity extends AppCompatActivity { private Spinner sp; private String[] data ={"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"}; private ArrayAdapter<String> adapter; private button enterBth; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); sp = (Spinner)findVIEwByID(R.ID.sp); enterBth = (button)findVIEwByID(R.ID.enter) ; adapter = new ArrayAdapter<String>(this, androID.R.layout.simple_spinner_item,data); adapter.setDropDownVIEwResource(androID.R.layout.simple_spinner_dropdown_item); sp.setAdapter(adapter); enterBth.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { int index = sp.getSelectedItemposition(); Log.d("Chinesezodiac","选中的生肖是"+index+data[index]);// 跳转 Intent intent = new Intent(MainActivity.this,SecondActivity.class);// int intent.putExtra("Chines",index);// 完成数据 传递 startActivity(intent);// 结束// MainActivity.this.finish(); } }); }}
5,编写 SecondActivity 类
package com.example.chinesezodiac;import androIDx.appcompat.app.AppCompatActivity;import androID.content.Intent;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.ImageVIEw;import androID.Widget.TextVIEw;import androID.Widget.Toast;public class SecondActivity extends AppCompatActivity implements VIEw.OnClickListener { private int index; private ImageVIEw imageVIEw; private TextVIEw textVIEw; private String[] data = {"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"}; private int[] imgData = {R.drawable.shu, R.drawable.niu, R.drawable.hu, R.drawable.tu, R.drawable.long1, R.drawable.she, R.drawable.ma, R.drawable.yang, R.drawable.hou, R.drawable.ji, R.drawable.gou, R.drawable.hjz,}; private button button; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_second);// 实例化 imageVIEw = (ImageVIEw) findVIEwByID(R.ID.img); textVIEw = (TextVIEw) findVIEwByID(R.ID.text); button = (button) findVIEwByID(R.ID.btn);// 得到 intent Intent intent = getIntent();// 传值;默认为0 index = intent.getIntExtra("Chines", 0);// 获取 文本 textVIEw.setText(data[index]);// 获取 图片 imageVIEw.setimageResource(imgData[index]);// 点击返回事件 button.setonClickListener(this); } @OverrIDe public voID onClick(VIEw v) { // 根据ID 判断 用户当前点的是那个按钮; switch (v.getID()) { case R.ID.btn:// 跳转 Intent intent = new Intent(SecondActivity.this, MainActivity.class);// intent.putExtra() startActivity(intent); break; } }}
6,项目路径
7,运行结果截图
总结以上是内存溢出为你收集整理的Android LinearLayout布局 实现十二生肖的跳转全部内容,希望文章能够帮你解决Android LinearLayout布局 实现十二生肖的跳转所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)