安卓星座计算源码

这个安卓星座计算小工具,需要两个类。在这里我就用first和sencond来命名。
first.java

public class first extends Activity {
    private Button bt1;
    private EditText ex;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first);
        bt1=(Button) findViewById(R.id.button1);
        ex=(EditText) findViewById(R.id.editText1);

        bt1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if("".equals(ex.getText().toString())){

                    Toast.makeText(first.this, "请输入阳历出生年月", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent=new Intent();
                intent.setClass(first.this, sncond.class);

                intent.putExtra("str",ex.getText().toString());

                startActivityForResult(intent, 1);

            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==1&&resultCode==1){
            Bundle bundle=data.getExtras();
            String str=bundle.getString("back");
            Toast.makeText(first.this, str, Toast.LENGTH_SHORT).show();

        }
    }
}

second.java

public class sencond extends Activity {
    private TextView tv;
private Button bt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sencond);
        bt=(Button) findViewById(R.id.button2);

        Intent intent=getIntent();
        Bundle bundle = intent.getExtras();
        String str=bundle.getString("str");
        tv=(TextView) findViewById(R.id.textView2);
        tv.setText(query(str));

        bt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent=new Intent();
                intent.setClass(sencond.this, first.class);
                intent.putExtra("back", "请输入您要计算的阳历出生年月");
                setResult(1,intent);
                finish();
            }
        });

    }
    public String query(String birthday)
    {
        int month=0;
        int day=0;
        month=Integer.parseInt((birthday.substring(5,7)));
        day=Integer.parseInt((birthday.substring(8,10)));

        String name="";
        if(month>0 &&month<13 && day>0 && day<32){
            if((month ==3 && day>20) || (month ==4 && day<21)){
                name="您是白羊座!";
            }else if((month ==4 && day>20) || (month ==5 && day<21)){
                name="您是金牛座!";
            }else if((month ==5 && day>20) || (month ==6 && day<22)){
                name="您是双子座!";
            }else if((month ==6 && day>21) || (month ==7 && day<23)){
                name="您是巨蟹座!";
            }else if((month ==7 && day>22) || (month ==8 && day<23)){
                name="您是狮子座!";
            }else if((month ==8 && day>22) || (month ==9 && day<23)){
                name="您是处女座!";
            }else if((month ==9 && day>22) || (month ==10 && day<23)){
                name="您是天枰座!";
            }else if((month ==10 && day>22) || (month ==11 && day<22)){
                name="您是天蝎座!";
            }else if((month ==11 && day>21) || (month ==12 && day<22)){
                name="您是射手座!";
            }else if((month ==12 && day>21) || (month ==1 && day<20)){
                name="您是摩羯座!";
            }else if((month ==1 && day>19) || (month ==2 && day<19)){
                name="您是水牛座!";
            }else if((month ==2 && day>18) || (month ==3 && day<21)){
                name="您是双鱼座!";
            }
            name=month+"月"+day +"日 " +name;
        }else{
            name="您输入的生日格式不正确或者不是真实生日!";
        }
        return name;
    }
}

另外的两个xml自己配置下就可以了。

你可能感兴趣的:(学习历程)