在线咨询
微信咨询
服务热线
服务热线:15639912513
TOP
当前位置:
首页 > 新闻中心> 安卓课堂>android显示意图实例

android显示意图实例

发布时间:2020-02-11 浏览:508次

w88app开发运用Android进行制作app时候,遇到显示意图intent。下面是关于意图的一个小案例,分享给大家。代码可以直接运行。

布局代码:

(activity_main.xml)

{?xml version="1.0" encoding="utf-8"?}

{RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"

                xmlns:app="https://schemas.android.com/apk/res-auto"

                xmlns:tools="https://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/rg_gender"

            android:layout_below="@id/et_name"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"}

        {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:text="女"

                /}

    {/RadioGroup}

    {Button

            android:id="@+id/btn_calc"

            android:layout_below="@id/rg_gender"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="计算人品"/}

{/RelativeLayout}

(activity_result.xml)布局代码

{?xml version="1.0" encoding="utf-8"?}

{RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"

                android:orientation="vertical" android:layout_width="match_parent"

                android:layout_height="match_parent"}

    {TextView

            android:id="@+id/tv_info"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:text="姓名:王晓冬 性别:男" /}

    {TextView

            android:id="@+id/tv_result"

            android:layout_below="@id/tv_info"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="计算人品"/}

{/RelativeLayout}

java代码:

package cn.xhhkj.xhhkjtest;


import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.RadioGroup;


public class MainActivity extends Activity implements OnClickListener{


    private EditText et_name;

    private RadioGroup rg_gender;

    private Button btn_calc;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        et_name =findViewById(R.id.et_name);

        rg_gender = findViewById(R.id.rg_gender);

        btn_calc =findViewById(R.id.btn_calc);

        btn_calc.setOnClickListener(this);


    }


    @Override

    public void onClick(View v) {

        String name = et_name.getText().toString().trim();

        boolean isMale = true;

        int id = rg_gender.getCheckedRadioButtonId();

        switch (id) {

            case R.id.rb_male:

                isMale = true;

                break;

            case R.id.rb_female:

                isMale = false;

                break;

        }

        Intent intent = new Intent(getApplicationContext(), ResultActivity.class);

        intent.putExtra("name",name);

        intent.putExtra("gender", isMale);

        startActivity(intent);

    }


}

 

(resultActivity)java代码

package cn.xhhkj.xhhkjtest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import java.util.Random;

public class ResultActivity extends Activity {
   private TextView tv_info;
   private TextView tv_result;
   @Override
   protected void onCreate(Bundle savedInstanceState){
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_result);
       tv_info = (TextView) findViewById(R.id.tv_info);
       tv_result = (TextView) findViewById(R.id.tv_result);
       Intent intent = getIntent();
       String name = intent.getStringExtra("name");
       boolean isMale = intent.getBooleanExtra("gender", true);
       tv_info.setText("姓名:"+name+"性别:"+(isMale ?"男":"女"));
       calcRP();
   }

   private void calcRP() {
       Random random = new Random();
       int result = random.nextInt(100);
       if(result>80){
           tv_result.setText("人品好....");
       }else if(result>60){
           tv_result.setText("人品及格");
       }else if(result>40){
           tv_result.setText("人品一般ֵ");
       }else{
           tv_result.setText("人品太差");
       }
   }
}


TAG
508
该内容对我有帮助