android显示意图实例
郑州app开发运用Android进行制作app时候,遇到显示意图intent。下面是关于意图的一个小案例,分享给大家。代码可以直接运行。
创新互联建站-专业网站定制、快速模板网站建设、高性价比林甸网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式林甸网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖林甸地区。费用合理售后完善,10年实体公司更值得信赖。
布局代码:
(activity_main.xml)
{?xml version="1.0" encoding="utf-8"?}
{RelativeLayout 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"
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="http://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("人品太差");
}
}
}
网站题目:android显示意图实例
网站网址:http://azwzsj.com/article/scdioc.html