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:textStyle="bold"
android:textColor="#000000"
android:textSize="18sp"
android:layout_margin="5dp" />
<EditText
android:id="@+id/edit_inputcost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="30sp"
android:layout_margin="5dp"
android:background="#D7a3f5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择日期:"
android:textStyle="bold"
android:textColor="#000000"
android:textSize="18sp"
android:layout_margin="5dp" />
<DatePicker
android:id="@+id/datePicker_birth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<Button
android:id="@+id/button_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="#ffff00"
android:text="查询"
android:textColor="#FFFFFFFF"
android:textSize="40sp"
android:textStyle="bold" />
</LinearLayout>
MainActivity
package com.example.firstwork;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//跳转
Button btn_search= findViewById(R.id.button_search);
DatePicker date_birth= findViewById(R.id.datePicker_birth);
EditText edit_inputcost=findViewById(R.id.edit_inputcost);
btn_search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent packageContext;
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
//数据传输
intent.putExtra("costput",edit_inputcost.getText());
intent.putExtra("year",date_birth.getYear());
intent.putExtra("yue",date_birth.getMonth());
intent.putExtra("ri",date_birth.getDayOfMonth());
startActivity(intent);
}
});
}
}
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">
<TextView
android:id="@+id/textView_getcost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示获取到的消费金额"
android:textStyle="bold"
android:textColor="#ffffff"
android:textSize="20sp"
android:layout_margin="5dp" />
<TextView
android:id="@+id/textView_getbirth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示获取到的日期"
android:textStyle="bold"
android:textColor="#ffffff"
android:textSize="20sp"
android:layout_margin="5dp" />
<TextView
android:id="@+id/textView_contentzodiac"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示打折数字"
android:textStyle="bold"
android:textColor="#ffffff"
android:textSize="20sp"
android:layout_margin="5dp" />
</LinearLayout>
SecondActivity
package com.example.firstwork;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.text.BreakIterator;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView textView_getcost = findViewById(R.id.textView_getcost);
TextView textView_getbirth = findViewById(R.id.textView_getbirth);
TextView textView_contentzodiac = findViewById(R.id.textView_contentzodiac);
int getcost = getIntent().getIntExtra("costput",0);
int year =getIntent().getIntExtra("year", 0);
int month= getIntent().getIntExtra("ri", 0);
int mydate= getIntent().getIntExtra("ri", 0);
textView_getcost.setText(getcost);
textView_getbirth.setText("所选日期是" + year + "年" + month + "月" + mydate + "日");
int[] contant={R.string.六折, R.string.六点五折, R.string.七折, R.string.七点五折, R.string.八折, R.string.八点五折, R.string.九折};
int i = find(getcost, mydate);
textView_contentzodiac.setText(contant[i]);
}
private int find(int mycost, int myday) {
int i = 0;
if(mycost==800&&myday>=1||mycost==1000&&myday<=31){i=0;}
if(mycost==600&&myday>=1||mycost<800&&myday<=31){i=1;}
if(mycost==400&&myday>=1||mycost<600&&myday<=31){i=2;}
if(mycost==200&&myday>=1||mycost<400&&myday<=31){i=3;}
if(mycost==100&&myday>=1||mycost<200&&myday<=31){i=4;}
if(mycost==90&&myday>=1||mycost<100&&myday<=31){i=5;}
if(mycost==0&&myday>=1||mycost<90&&myday<=31){i=6;}
return i;
}
}
strings.xml:
<resources>
<string name="app_name">firstwork</string>
<string name="六折">打6折</string>
<string name="六点五折">打6.5折</string>
<string name="七折">打7折</string>
<string name="七点五折">打7.5折</string>
<string name="八折">打8折</string>
<string name="八点五折">打8.5折</string>
<string name="九折">打9折</string>
</resources>
代码没有报错,但是第一个界面的查询按钮点击后闪退出去,请问原因在哪里?