安卓案例:人品計算機

來源:互聯網
上載者:User

標籤:super   mat   cal   跳轉   Nid   簡單的   width   exce   edit   

顯跳轉的運用

 

兩個簡單的布局:

main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity" >    <EditText        android:id="@+id/et_name"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="請輸入姓名:" />    <RadioGroup        android:id="@+id/radioGroup1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <RadioButton            android:id="@+id/rb_male"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="男" />        <RadioButton            android:id="@+id/rb_female"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="25dp"            android:text="女" />    </RadioGroup>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="click"        android:text="計算" /></LinearLayout>

 

result:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/tv_name"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/tv_sex"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/tv_result"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></LinearLayout>

 

MainActivity:

package org.dreamtech.game;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.text.TextUtils;import android.view.View;import android.widget.EditText;import android.widget.RadioGroup;import android.widget.Toast;public class MainActivity extends Activity {    private EditText et_name;    private RadioGroup rg_group;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        et_name = (EditText) findViewById(R.id.et_name);        rg_group = (RadioGroup) findViewById(R.id.radioGroup1);    }    public void click(View v) {        String name = et_name.getText().toString().trim();        if (TextUtils.isEmpty(name)) {            Toast.makeText(getApplicationContext(), "請輸入姓名", Toast.LENGTH_LONG)                    .show();            return;        }        int radiobuttonID = rg_group.getCheckedRadioButtonId();        int sex = 0;        switch (radiobuttonID) {        case R.id.rb_male:            sex = 1;            break;        case R.id.rb_female:            sex = 2;            break;        }        if (sex == 0) {            Toast.makeText(getApplicationContext(), "請選擇性別", Toast.LENGTH_LONG)                    .show();            return;        }        Intent intent = new Intent(this, ResultActivity.class);                intent.putExtra("name",name);        intent.putExtra("sex", sex);                startActivity(intent);    }}

 

 

ResultActivity:

package org.dreamtech.game;import java.io.UnsupportedEncodingException;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.widget.TextView;public class ResultActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_result);        TextView tv_name = (TextView) findViewById(R.id.tv_name);        TextView tv_sex = (TextView) findViewById(R.id.tv_sex);        TextView tv_result = (TextView) findViewById(R.id.tv_result);        Intent intent = getIntent();        String name = intent.getStringExtra("name");        byte[] bytes = name.getBytes();        int sex = intent.getIntExtra("sex", 0);        tv_name.setText(name);        switch (sex) {        case 1:            tv_sex.setText("男");            try {                bytes = name.getBytes("gbk");            } catch (UnsupportedEncodingException e) {            }            break;        case 2:            tv_sex.setText("女");            try {                bytes = name.getBytes("utf-8");            } catch (UnsupportedEncodingException e) {            }            break;        }        int total = 0;        for (byte b : bytes) {            int number = b & 0xff;            total += number;        }        int score = Math.abs(total) % 100;        if (score > 90) {            tv_result.setText("您的人品很好");        } else if (score > 80) {            tv_result.setText("您的人品不錯");        } else if (score > 60) {            tv_result.setText("您的人品一般");        } else {            tv_result.setText("您的人品很差");        }    }}

 

安卓案例:人品計算機

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.