通过DatePicker选择出生日期,简易地对生肖进行查询的app
查询结果:
缺点:出生日期选择较为麻烦。
代码如下:
MainActivity:
package com.example.myapplication;
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 {
Button btn_search;
EditText edit_inputname;
DatePicker date_birth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_search = findViewById(R.id.button_search);
edit_inputname = findViewById(R.id.edit_inputname);
date_birth = findViewById(R.id.datePicker_birth);
btn_search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("name", edit_inputname.getText().toString());
intent.putExtra("year", date_birth.getYear());
intent.putExtra("yue", date_birth.getMonth());
intent.putExtra("ri", date_birth.getDayOfMonth());
startActivity(intent);
}
});
}
}
SecondActivity:
package com.example.myapplication;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
TextView txt_getname, txt_getbirth, txt_contentstar;
ImageView img_imgstar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
txt_getname = findViewById(R.id.textView_getName);
txt_getbirth = findViewById(R.id.textView_getbirth);
txt_contentstar = findViewById(R.id.textView_contentstar);
img_imgstar = findViewById(R.id.imageView_imgstar);
String str1 = getIntent().getStringExtra("name");
int myyear = getIntent().getIntExtra("year", 0);
int mymonth1 = getIntent().getIntExtra("yue", 0);
int mymonth = mymonth1 + 1;
int myday = getIntent().getIntExtra("ri", 0);
txt_getname.setText("您好:" + str1);
txt_getbirth.setText("您的出生日期为:" + myyear + "年" + mymonth + "月" + myday + "日");
int[] imgarr = {R.drawable.shu, R.drawable.niu, R.drawable.hu, R.drawable.tu,
R.drawable.lo, R.drawable.she, R.drawable.ma, R.drawable.yang,
R.drawable.hou, R.drawable.ji, R.drawable.gou, R.drawable.zhu};
int[] contentarr = {R.string.鼠, R.string.牛, R.string.虎, R.string.兔, R.string.龙, R.string.蛇,
R.string.马, R.string.羊, R.string.猴, R.string.鸡, R.string.狗, R.string.猪};
int i = find(myyear,mymonth);
img_imgstar.setImageResource(imgarr[i]);
txt_contentstar.setText(contentarr[i]);
}
private int find(int myyear,int mymonth) {
int i = 0;
int myyear1 = (myyear - 1900)%12;
if (myyear1 == 0) {
i = 0;
}
if (myyear1 == 1) {
i = 1;
}
if (myyear1 == 2){
i = 2;
}
if (myyear1 == 3){
i = 3;
}
if (myyear1 == 4){
i = 4;
}
if (myyear1 == 5){
i = 5;
}
if (myyear1 == 6){
i = 6;
}
if (myyear1 == 7){
i = 7;
}
if (myyear1 == 8){
i = 8;
}
if (myyear1 == 9){
i = 9;
}
if (myyear1 == 10){
i = 10;
}
if (myyear1 == 11){
i = 11;
}
return i;
}
}
自行找到生肖的图片添加到drawable文件中。
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:background="@drawable/b_jing1"
android:orientation="vertical"
tools:context=".MainActivity"
>
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入您的姓名:"
android:textSize="22sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_margin="5dp"/>
<EditText
android:id="@+id/edit_inputname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#D8EBC5"
android:gravity="center"
android:text=""
android:textColor="#000000"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="选择您的出生日期:"
android:textColor="#000000"
android:textSize="22sp"
android:textStyle="bold" />
<DatePicker
android:id="@+id/datePicker_birth"
android:layout_width="wrap_content"
android:layout_height="366dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="4dp"
android:background="#C1E4C1"
/>
<Button
android:id="@+id/button_search"
android:layout_width="227dp"
android:layout_height="61dp"
android:layout_gravity="center"
android:layout_margin="30dp"
android:background="@drawable/btn_bground"
android:text="生肖测算"
android:textColor="#000000"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
Second_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity"
android:background="@drawable/b_jing1">
<TextView
android:id="@+id/textView_getName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="#000000"
android:background="#D8EBC5"
android:layout_margin="10dp"
android:text="显示获得到的用户名" />
<TextView
android:id="@+id/textView_getbirth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textStyle="bold"
android:background="#D8EBC5"
android:textColor="#000000"
android:layout_margin="5dp"
android:text="显示获得到的出生日期"/>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textStyle="bold"
android:background="#D8EBC5"
android:textColor="#000000"
android:layout_margin="5dp"
android:text="您的属相是:" />
<ImageView
android:id="@+id/imageView_imgstar"
android:layout_width="230dp"
android:layout_height="211dp"
android:layout_gravity="center"
android:layout_margin="24dp"
tools:srcCompat="@tools:sample/backgrounds/scenic" />
<TextView
android:id="@+id/textView_contentstar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="#000000"
android:background="#D8EBC5"
android:layout_margin="14dp"
android:text="显示生肖的性格特点" />
</LinearLayout>
将strings中的内容修改:
<resources>
<string name="app_name">My Application</string>
<string name="鼠">老鼠偷吃粮食,证明“仓鼠有余粮”,说明这户富足,家中鼠多象征富裕。吐宝鼠是藏传佛教中财神的誓言物,黄财神、财宝天王、毗沙门天王怀中都抱有吐宝鼠。吐宝鼠原生活海中,天、人、龙三界所有摩尼宝全部是吐宝鼠吐出来的。</string>
<string name="牛">因为牛耕,中国人对牛感情渐深,把诸如憨厚勤劳,不求回报等优秀品质附在牛身上,鲁迅以“俯首甘为孺子牛”言志。</string>
<string name="虎">虎是百兽之王,寅虎相配,甲骨文“寅”字如箭矢状,《说文》中“寅”意为“阳气上升,虽上有冻土,必破土而出”,与虎的凶猛刚阳之气契合。</string>
<string name="兔">嫦娥奔月传说源于《淮南子.览冥训》,嫦娥在月中有玉兔相伴。兔是月的象征。</string>
<string name="龙">龙是行云布雨之神。龙象征出人头地,不同凡响。</string>
<string name="蛇">蛇多以负面形象出现。“人心不足蛇吞象”形容蛇的贪婪;《农夫和蛇》写出蛇的狡猾和冷血;狠毒之人被形容为“蛇蝎心肠”。</string>
<string name="马">作为六畜之首,马除用作交通运输,还是强大的军事装备。战国以“万乘之国”,“千乘之国”等马拉战车的数量来形容国力强弱</string>
<string name="羊">古人视羊为“德畜”,善群、好仁、死义、知礼。善群,羊喜欢聚群。好仁指羊善良,有角但不好斗。</string>
<string name="猴">猴与”侯“同音,猴子骑马寓意马上封侯;猴向枫树上挂印寓意封侯挂印;一只猴子骑在另一只猴子背上寓意辈辈封侯。</string>
<string name="鸡">《韩诗外传》概括鸡的“五德”,“头戴冠,文也;足搏距,武也;见敌敢干,勇也;见食相呼,义也;守夜不失时,信也”。故鸡有德禽的雅号。</string>
<string name="狗">狗的忠诚,传说和现实比比皆是。《述异记》载,魏晋陆机的“黄耳”,为主人送信,劳累而亡。《搜神记》中的黑龙犬,在火中用水沾湿醉酒的主人使其幸存。</string>
<string name="猪">猪有“乌金”之名,父系氏族公社时期,猪是财富标志,临夏大何庄的墓葬有三十六块猪骨陪葬。俗信猪能预兆雨水。</string>
</resources>