Android之使用Bundle来传递数据
嗯还是令人蛋疼的实验4,这丫还有第二部分;就是根据你选择的生日来显示你的星座这个功能:
先发上码云连接:https://gitee.com/bbchond/Exp4_part2_Bundle
(我知道实验有个bug没改,但是谁叫老夫演示完成了呢hhhhhh)
星座显示的页面
先上第一个页面的layout布局代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/nameTip"
android:textColor="#000000"
android:textSize="20sp"
android:text="请输入您的姓名:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="44dp"
android:layout_marginTop="52dp" />
<EditText
android:id="@+id/nameTypeIn"
android:layout_below="@id/nameTip"
android:layout_width="250sp"
android:layout_height="wrap_content"
android:layout_marginStart="44dp"/>
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:calendarViewShown="false"
android:datePickerMode="spinner"
android:layout_marginTop="31dp"
android:layout_below="@+id/nameTypeIn"
android:layout_centerHorizontal="true">
</DatePicker>
<Button
android:id="@+id/button"
android:layout_width="200sp"
android:layout_height="wrap_content"
android:text="查询"
android:gravity="center"
android:layout_below="@+id/datePicker"
android:layout_alignEnd="@+id/nameTypeIn"
android:layout_marginTop="20dp"
android:background="@mipmap/btn_background"/>
<TextView
android:id="@+id/showDate"
android:text="点击查询看看你的星座性格吧"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="55dp" />
</RelativeLayout>
简述:可以看到我们使用android:datePickerMode="spinner"
这一行代码为DatePicker设定了格式(我觉得实验报告里的那个太丑了给换成这个了),使用android:calendarViewShown="false"
这一行代码使默认开启的日历视图不显示出来,达成了如下图的显示效果:
然后是JAVA代码部分:
public class MainActivity extends AppCompatActivity {
EditText editText;
private Button button;
DatePicker datePicker;
int mYear;
int mMonth;
int mDay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar calendar = Calendar.getInstance(Locale.CHINA);
mYear = calendar.get(Calendar.YEAR);
mMonth = calendar.get(Calendar.MONTH);
mDay = calendar.get(Calendar.DAY_OF_MONTH);
editText = (EditText) findViewById(R.id.nameTypeIn);
button = (Button) findViewById(R.id.button);
datePicker = (DatePicker) findViewById(R.id.datePicker);
datePicker.init(mYear, mMonth, mDay, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (TextUtils.isEmpty(editText.getText().toString())){
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("错误");
dialog.setMessage("请输入姓名");
dialog.setPositiveButton("确定",null);
dialog.show();
} else {
Intent intent = new Intent(MainActivity.this,ResultActivity.class);
Bundle bundle = new Bundle();
bundle.putString("userName",editText.getText().toString());
bundle.putInt("day",mDay);
bundle.putInt("month",(mMonth+1));
intent.putExtras(bundle);
startActivity(intent);
}
}
});
}
}
在这里我们首先实例化datePicker,然后使用datePicker.init
方法,将我们选中的年、月、日分别传入我们在一开始int的mYear,mMonth,mDay
,代码示例如下:
datePicker = (DatePicker) findViewById(R.id.datePicker);
datePicker.init(mYear, mMonth, mDay, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// textView.setText("你选择的日期是: " + year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
}
});
(注:monthOfYear默认的数值实际上是选中的月份的数值-1)
下面就是使用Bundle来传递数据的部分了:
Intent intent = new Intent(MainActivity.this,ResultActivity.class);
Bundle bundle = new Bundle();
bundle.putString("userName",editText.getText().toString());
bundle.putInt("day",mDay);
bundle.putInt("month",(mMonth+1));
intent.putExtras(bundle);
startActivity(intent);
可以看到的是,我们先将Bundle实例化,然后把获取到的数据放入Bundle之中,最后再使用intent.putExtras
方法直接传递bundle给下一个活动。
接下来就是第二个Activity部分:
首先还是layout布局文件的xml代码吧:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_marginTop="20sp"
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/userName"
android:text="rose"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" />
<TextView
android:textSize="20sp"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=",您的星座性格大概如下:" />
</LinearLayout>
<ImageView
android:id="@+id/constellation_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/layout1"
android:layout_marginTop="10sp"
android:layout_centerHorizontal="true"
android:src="@mipmap/star1"/>
<TextView
android:textSize="20sp"
android:id="@+id/constellation_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/constellation_image"
android:layout_marginTop="10sp"
android:text="白羊座 3月21日-4月19日 自我意识和主观意识很强,充满自信而且固执;不会等待机会从天而降,而会积极的争取或制造,无畏艰难和困苦。虽然有时会显得冲动,但基本上还是会保持理智和果决,是个适合面对竞争压力、热情且永远天真未泯的人。"/>
</RelativeLayout>
(这部分不讲了,该会了)
#######然后是JAVA代码部分:
public class ResultActivity extends AppCompatActivity {
TextView tv_name;
TextView tv_content;
ImageView imageView;
int getMonth;
int getDay;
private String[] constellationFiles = {"star12.txt","star1.txt","star2.txt","star3.txt","star4.txt",
"star5.txt","star6.txt","star7.txt","star8.txt","star9.txt","star10.txt",
"star11.txt"};
private int[] constellationImages = {R.mipmap.star12,R.mipmap.star1,R.mipmap.star2,R.mipmap.star3,R.mipmap.star4,
R.mipmap.star5,R.mipmap.star6,R.mipmap.star7,R.mipmap.star8,R.mipmap.star9,
R.mipmap.star10,R.mipmap.star11};
String[] constellationName = new String[]{"摩羯座", "水瓶座", "双鱼座", "白羊座", "金牛座",
"双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座"};
int[] constellationDivider = new int[]{20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
tv_name = (TextView) findViewById(R.id.userName);
tv_content = (TextView) findViewById(R.id.constellation_text);
imageView = (ImageView) findViewById(R.id.constellation_image);
Bundle bundle = this.getIntent().getExtras();
tv_name.setText(bundle.getString("userName"));
getMonth = bundle.getInt("month");
getDay = bundle.getInt("day");
getConstellation(getMonth,getDay);
}
private String getConstellation(int month, int day){
int index = getMonth;
if (month ==12 && day >= 22){
imageView.setImageResource(R.mipmap.star12);
String str = readFromAssets("star12.txt");
tv_content.setText(str);
} else {
if (day < constellationDivider[month - 1]){
index = index -1;
}
imageView.setImageResource(constellationImages[index]);
String str = readFromAssets(constellationFiles[index]);
tv_content.setText(str);
}
return constellationName[index];
}
public String readFromAssets(String fileName){
String result = "";
try {
InputStream in = getResources().getAssets().open(fileName);
byte[] buffer = new byte[in.available()];
in.read(buffer);
result = new String(buffer);
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
由于该Activity是由MainActivity跳转过来的,我们们先获取一下上个Activity传递过来的信息:
tv_name = (TextView) findViewById(R.id.userName);
tv_content = (TextView) findViewById(R.id.constellation_text);
imageView = (ImageView) findViewById(R.id.constellation_image);
Bundle bundle = this.getIntent().getExtras();
tv_name.setText(bundle.getString("userName"));
getMonth = bundle.getInt("month");
getDay = bundle.getInt("day");
可以看到我们在这里根据上一个Activity中的EditText中输入的字符来设置此处的用户名:
输入框
第二个Activity中的显示
然后就是资源读取部分了:
public String readFromAssets(String fileName){
String result = "";
try {
InputStream in = getResources().getAssets().open(fileName);
byte[] buffer = new byte[in.available()];
in.read(buffer);
result = new String(buffer);
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
(没啥好讲的)
然后写一个根据获取到的月日来判断星座的方法:
private String getConstellation(int month, int day){
int index = getMonth;
if (month ==12 && day >= 22){
imageView.setImageResource(R.mipmap.star12);
String str = readFromAssets("star12.txt");
tv_content.setText(str);
} else {
if (day < constellationDivider[month - 1]){
index = index -1;
}
imageView.setImageResource(constellationImages[index]);
String str = readFromAssets(constellationFiles[index]);
tv_content.setText(str);
}
return constellationName[index];
}
(不懂的就百度,第一个if (month ==12 && day >= 22)
是为了防止数组越界写的)
最后在onCreate方法里添加这样一行:
getConstellation(getMonth,getDay);