Android之使用Bundle来传递数据

嗯还是令人蛋疼的实验4,这丫还有第二部分;就是根据你选择的生日来显示你的星座这个功能:

先发上码云连接:https://gitee.com/bbchond/Exp4_part2_Bundle
(我知道实验有个bug没改,但是谁叫老夫演示完成了呢hhhhhh)

姓名输入、生日选择页面

星座显示的页面

先上第一个页面的layout布局代码:



    

    

    
    

    

简述:可以看到我们使用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代码吧:




    

        
        

    


    

    


(这部分不讲了,该会了)

#######然后是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);

好的完工趴窝歇逼了,待老夫试试板蓝根+黑片的药效

你可能感兴趣的:(Android之使用Bundle来传递数据)