1. 程式人生 > >android:根據使用者輸入的陽曆生日判斷所屬星座,並將判定結果在另一個介面上顯示給使用者

android:根據使用者輸入的陽曆生日判斷所屬星座,並將判定結果在另一個介面上顯示給使用者

1,主頁面程式碼:

package com.example.siwux.myapplication;

import
android.content.Intent;
import
android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import
android.widget.EditText;

public class
MainActivity extends AppCompatActivity {
   

private Button Button_queding;
    private
EditText editText_yuefen;
    private
EditText editText_ri;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
       
setContentView(R.layout.activity_main);
       
Button_queding = (Button)findViewById(R.id.
button_queding);
       
editText_yuefen = (EditText) findViewById(R.id.editText_yuefen);
       
editText_ri = (EditText) findViewById(R.id.editText_ri);
       
Button_queding.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
                Intent intent =
new Intent(MainActivity.this,openActivity.class);
               
String a=editText_yuefen.getText().toString().trim();
               
String q=editText_ri.getText().toString().trim();
               
intent.putExtra("w",a);
               
intent.putExtra("e",q);
               
startActivity(intent);
           
}
        })
;
   
}

佈局檔案程式碼activity_mian.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
   
android:layout_width="match_parent"
   
android:layout_height="wrap_content"
   
android:weightSum="1">

    <LinearLayout
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:orientation="horizontal"
       
android:layout_marginLeft="10dp"
       
android:layout_marginRight="10dp"
       
android:layout_marginTop="17dp"

       
android:weightSum="1"
       
android:id="@+id/linearLayout2"
       
android:layout_alignParentTop="true"
       
android:layout_centerHorizontal="true">

        <TextView
           
android:id="@+id/textView"
           
android:layout_width="wrap_content"
            
android:layout_height="wrap_content"
           
android:padding="10dp"
           
android:text="出生日期:"
           
android:textColor="#000000"
           
android:textSize="20dp" />

        <EditText
           
android:id="@+id/editText_yuefen"
           
android:layout_width="43dp"
           
android:layout_height="wrap_content"
           
android:layout_marginLeft="5dp"
           
android:hint="01"
           
android:textColor="#000000"
           
android:textSize="25dp"
           
android:textStyle="italic" />

        <TextView
           
android:id="@+id/textView1"
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:padding="10dp"
           
android:text="月"
           
android:textSize="20dp"
           
android:textColor="#000000"/>

        <EditText
           
android:id="@+id/editText_ri"
           
android:layout_width="43dp"
           
android:layout_height="wrap_content"
           
android:layout_marginLeft="5dp"
           
android:hint="01"
           
android:textColor="#000000"
           
android:textSize="25dp"
           
android:textStyle="italic" />

        <TextView
           
android:id="@+id/textView3"
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:padding="10dp"
           
android:text="日"
           
android:textSize="20dp"
           
android:textColor="#000000"/>

    </LinearLayout>

    <Button
       
android:id="@+id/button_queding"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:layout_marginRight="10dp"
       
android:layout_marginLeft="100dp"
       
android:layout_weight="1"
       
android:background="#3C8DC4"
       
android:gravity="center"
       
android:text="確定"
       
android:textColor="#ffffff"
       
android:textSize="25dp"
       
android:layout_marginTop="85dp"
       
android:layout_below="@+id/linearLayout2"
       
android:layout_centerHorizontal="true" />

</RelativeLayout>

在建立一個開啟的openActivity,jaca

package com.example.siwux.myapplication;

import
android.content.Intent;
import
android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.widget.EditText;

public class
openActivity extends AppCompatActivity {
   
private EditText  editText_2;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
       
setContentView(R.layout.activity_mainopen);
       
editText_2 = (EditText) findViewById(R.id.editText_2);
       
Intent inter =getIntent();

       
String p = getIntent().getStringExtra("w");
       
String q =getIntent().getStringExtra("e");
       
String r="20"; //
       
String y="21";
       
String u="22";
       
String u1="23";
       
String u2="24";
        int
i =q.compareTo(r);
        int
er=q.compareTo(y);
        int
san=q.compareTo(u);
        int
si=q.compareTo(u1);
        int
wu=q.compareTo(u2);
        if
(p.equals("01") ){
           
if (er>=0){
               
editText_2.setText("水瓶座");
           
}
           
else {
               
editText_2.setText("魔蠍座");
           
}
        }
       
if(p.equals("02")){
           
if (i >= 0){
               
editText_2.setText("雙魚座");
           
}
           
else {
               
editText_2.setText("水瓶座");
           
}
        }
       
if(p.equals("03")){
            
if (er>=0){
               
editText_2.setText("白羊座");
           
}
           
else {
               
editText_2.setText("雙魚座");
           
}
        }
       
if(p.equals("04")){
           
if (er>=0){
               
editText_2.setText("金牛座");
           
}
           
else {
               
editText_2.setText("白羊座");
           
}
        }
       
if(p.equals("05")){
           
if (san>=0){
               
editText_2.setText("雙子座");
           
}
           
else {
               
editText_2.setText("金牛座");
           
}
        }
       
if(p.equals("06")){
           
if (san>=0){
               
editText_2.setText("巨蟹座");
           
}
           
else {
               
editText_2.setText("雙子座");
           
}
        }
       
if(p.equals("07")){
           
if (si>=0){
               
editText_2.setText("獅子座");
           
}
           
else {
               
editText_2.setText("巨蟹座");
           
}
        }
       
if(p.equals("08")){
           
if (wu>=0){
               
editText_2.setText("處女座");
           
}
           
else {
               
editText_2.setText("獅子座");
           
}
        }
       
if(p.equals("09")){
           
if (wu>=0){
               
editText_2.setText("天秤座");
           
}
           
else {
                
editText_2.setText("處女座");
           
}
        }
       
if(p.equals("10")){
           
if (wu>=0){
               
editText_2.setText("天蠍座");
           
}
           
else {
               
editText_2.setText("天秤座");
           
}
        }
        
if(p.equals("11")){
           
if (si>=0){
               
editText_2.setText("射手座");
           
}
           
else {
               
editText_2.setText("天蠍座");
           
}
        }
       
if(p.equals("12")){
           
if (si>=0){
                
editText_2.setText("魔蠍座");
           
}
           
else {
               
editText_2.setText("射手座");
           
}
        }
    }
}

佈局檔案:activity_mianopen.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
   
android:orientation="vertical"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent">

    <TextView
       
android:id="@+id/textView2"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="您的星座為:"
       
android:textColor="#000000"
       
android:textSize="25dp"/>

    <EditText
       
android:id="@+id/editText_2"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:hint="星座"
       
android:textColor="#000000"
       
android:textStyle="italic" />

</LinearLayout>