android人品计算器示例

android人品计算器示例需求描述:输入姓名,年龄,性别之后计算相应的人品效果展示代码详情布局activity_main.xml<?xmlversion="1.0"encoding="utf-8"?><android.support.constraint.ConstraintLayoutxmlns:android="http://schemas.android.com/a…

需求描述: 输入姓名,年龄,性别之后 计算相应的人品

效果展示

在这里插入图片描述
在这里插入图片描述

代码详情
  • 布局 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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" tools:context=".MainActivity">


    <EditText android:id="@+id/editText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:ems="10" android:hint="姓名" android:inputType="textPersonName" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />

    <EditText android:id="@+id/editText2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="30dp" android:layout_marginEnd="8dp" android:ems="10" android:hint="年龄" android:inputType="textPersonName" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editText" />

    <RadioGroup android:id="@+id/radioGroup" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="30dp" android:layout_marginEnd="8dp" android:orientation="horizontal" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editText2">

        <RadioButton android:id="@+id/radioButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:checked="true" android:text="" />

        <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="" />

        <RadioButton android:id="@+id/radioButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="未知" />
    </RadioGroup>

    <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="30dp" android:layout_marginEnd="8dp" android:text="计算" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/radioGroup" android:onClick="click"/>
</android.support.constraint.ConstraintLayout>

activity_result.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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" tools:context=".ResultActivity">

    <TextView android:id="@+id/textView" style="@style/LeftKey" android:layout_marginStart="8dp" android:text="姓名" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />

    <TextView android:id="@+id/textView2" style="@style/RightVal" android:layout_width="0dp" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:gravity="center_vertical" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/textView" app:layout_constraintTop_toTopOf="parent" tools:text="teeee" />

    <TextView android:id="@+id/textView3" style="@style/LeftKey" android:layout_marginStart="8dp" android:text="年龄" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView" />

    <TextView android:id="@+id/textView4" style="@style/RightVal" android:layout_width="0dp" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/textView3" app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <TextView android:id="@+id/textView5" style="@style/LeftKey" android:layout_marginStart="8dp" android:text="性别" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView3" />

    <TextView android:id="@+id/textView6" style="@style/RightVal" android:layout_width="0dp" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/textView5" app:layout_constraintTop_toBottomOf="@+id/textView4" />

    <TextView android:id="@+id/textView7" style="@style/LeftKey" android:layout_marginStart="8dp" android:text="分数" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView5" />

    <TextView android:id="@+id/textView8" style="@style/RightVal" android:layout_width="0dp" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/textView7" app:layout_constraintTop_toBottomOf="@+id/textView6" />

    <TextView android:id="@+id/textView9" style="@style/LeftKey" android:gravity="start" android:padding="10dp" android:layout_width="0dp" android:layout_height="200dp" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:hint="评论" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView7" />
</android.support.constraint.ConstraintLayout>
  • 逻辑控制
    MainActivity
package com.example.www;

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

public class MainActivity extends AppCompatActivity { 
   

    private EditText mEtName;
    private EditText mEtAge;
    private RadioGroup mRgSex;

    @Override
    protected void onCreate(Bundle savedInstanceState) { 
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mEtName = (EditText) findViewById(R.id.editText);
        mEtAge = (EditText) findViewById(R.id.editText2);
        mRgSex = (RadioGroup) findViewById(R.id.radioGroup);

    }

    public void click(View view) { 
   
        String name = mEtName.getText().toString().trim();
        String ageCon = mEtAge.getText().toString().trim();
        String sexCon = ((RadioButton)findViewById(mRgSex.getCheckedRadioButtonId())).getText().toString().trim();

        if(name.isEmpty()) { 
   
            Toast.makeText(this, "请输入姓名", Toast.LENGTH_LONG).show();
            return;
        }

        if(ageCon.isEmpty() || !ageCon.matches("^[0-9]*$")) { 
   
            Toast.makeText(this, "您输入的年龄不正确", Toast.LENGTH_LONG).show();
            return;
        }

        int age = Integer.parseInt(ageCon);

        Intent intent = new Intent(this, ResultActivity.class);
        intent.putExtra("age", age);
        intent.putExtra("name", name);
        intent.putExtra("sex", sexCon);
        startActivity(intent);
    }
}

ResultActivity

package com.example.www;

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

public class ResultActivity extends AppCompatActivity { 
   

    @Override
    protected void onCreate(Bundle savedInstanceState) { 
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);

        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        int age = intent.getIntExtra("age", 0);
        String sex = intent.getStringExtra("sex");

        System.out.println(name + "--" + age + "--" + sex);
        initView(name, sex, age);
    }

    public void initView(String name, String sex, int age){ 
   
        TextView tvName = (TextView) findViewById(R.id.textView2);
        TextView tvAge = (TextView) findViewById(R.id.textView4);
        TextView tvSex = (TextView) findViewById(R.id.textView6);
        TextView tvScore = (TextView) findViewById(R.id.textView8);
        TextView tvComment = (TextView) findViewById(R.id.textView9);
        int score = calcScore(name);
        tvName.setText(name);
        tvAge.setText(new Integer(age).toString());
        tvSex.setText(sex);
        tvScore.setText(new Integer(calcScore(name)).toString());
        tvComment.setText(comment(score));
    }

    public int calcScore(String name) { 
   
        byte[] names = name.getBytes();
        int total = 0;
        for (byte b : names) { 
   
            int temp = b & 0xff;
            total += temp;
        }

        return Math.abs(total) % 100;
    }

    public String comment(int score){ 
   
        if(score > 90) { 
   
            return "您的人品已经爆表了";
        } else if(score > 80) { 
   
            return "您的人品非常棒";
        } else if(score > 70) { 
   
            return "您的人品还算可以";
        } else if(score > 60) { 
   
            return "您的人品刚刚及格";
        } else { 
   
            return "您的人品挂了,你小子坏得很……";
        }
    }
}

配置文件

AndroidManifest.xml

这个文件中的 android:icon="@mipmap/ic_launcher_foreground" 需要替换成 你自己的 图标或者修改为系统默认的图标才行,不然会报错

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.www">

    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher_foreground" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ResultActivity"></activity>
    </application>

</manifest>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/37242.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注