目标和思路
目标:
做一个有多级页面的app,用户选择需要选择的信息:性别、年龄、姓名。点击提交,会根据随机生成一个三国时期的人物与其对应。并提示相关信息。
思路:
编写多个页面,通过intent组件实现页面跳转,并在MainActivity.class文件中根据选项的选择随机跳转页面,通过if判断性别,通过Math.random()函数生成一个1-5的随机数,根据随机数改变页面的数据,包括人物的名字和图片信息,5男5女。并用intent组件把姓名数据传送给新打开的页面,并打印出来。
源代码
Talk is poor,show me the code。
界面xml:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/sex_word" //添加ID
android:layout_width="match_parent" //充满页面
android:layout_height="wrap_content" //适应内容
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="30dp"
android:textSize="20dp"
android:textColor="#f40"
android:text="请选择您的性别:" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" //水平对齐
android:layout_marginTop="20dp"
android:id="@+id/Get_sex">
<RadioButton
android:id="@+id/rebt_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="男神" />
<RadioButton
android:id="@+id/rebt_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="女神" />
</RadioGroup>
<TextView
android:id="@+id/age_word"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="30dp"
android:textSize="20dp"
android:textColor="#f40"
android:text="请选择您的年龄段:" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:id="@+id/Get_age">
<RadioButton
android:id="@+id/young_person"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="20岁以下" />
<RadioButton
android:id="@+id/middle_person"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="20-50" />
<RadioButton
android:id="@+id/old_person"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="50岁以上" />
</RadioGroup>
<TextView
android:id="@+id/user_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="30dp"
android:textSize="20dp"
android:textColor="#f40"
android:text="请输入您的姓名:" />
<EditText
android:id="@+id/get_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:padding="10dp"
android:textColor="#fff"
android:background="#999"
/>
<Button
android:id="@+id/submit_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:text="点击提交" />
</LinearLayout>
activity_liubei.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">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/PageImg"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@drawable/liubei"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> //垂直对齐
<TextView
android:id="@+id/user_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="350dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="140dp"
android:textSize="40dp"
android:textColor="#003300"
android:text="TextView" />
<TextView
android:id="@+id/LiuText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:text="您的前世是:"
android:textColor="#CC3333" //字体颜色
android:textSize="40dp" //字体大小
tools:layout_editor_absoluteX="152dp"
tools:layout_editor_absoluteY="16dp" />
<TextView
android:id="@+id/rows_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="160dp"
android:textColor="#336699"
android:textSize="40dp"
android:text="刘备" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lengxiaohua.works">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
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=".LiuBei"></activity> //注册页面
</application>
</manifest>
Java源代码:
MainActivity.java:
package com.example.lengxiaohua.works;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
//主界面函数,通过这个页面请用户选择性别、年龄段和输入姓名,任何把获取的数据传到新打开的页面使用。
public class MainActivity extends AppCompatActivity {
private RadioGroup Get_sex,Get_age; //定义两个用来获取单选框数据对象的变量
private String sex; //定义用来储存从单选框获得的性别的信息
private String age; //定义用来储存从单选框获得的年龄段的信息
private Button btn; //定义用来获取按钮对象的变量
private EditText username; //定义用来获取输入框对象的变量
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//两个默认生成的函数。
Get_sex = (RadioGroup) findViewById(R.id.Get_sex);
Get_age = (RadioGroup) findViewById(R.id.Get_age);
btn = (Button) findViewById(R.id.submit_btn);
username = (EditText) findViewById(R.id.get_name);
//通过id获取xml文件页面的各个元素的对象
Get_sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.rebt_male){
sex = "male";
}else if (checkedId == R.id.rebt_female){
sex = "female";
}
}
});
//这是一个单选框获取当选择改变时触发的事件,然后做相应处理的一种简便的方法。这里选择用户选择的性别,并且我为它赋值为一个字符串。
Get_age.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.young_person){
age = "young";
}else if (checkedId == R.id.middle_person) {
age = "middle";
}else if (checkedId == R.id.old_person){
age = "old";
}
}
});
//这是一个单选框获取当选择改变时触发的事件,然后做相应处理的一种简便的方法。这里选择用户选择的年龄段,并且我为它赋值为一个字符串。
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,LiuBei.class);
intent.putExtra("userage",age);
intent.putExtra("usersex",sex);
intent.putExtra("username",username.getText().toString().trim());
startActivity(intent);
}
});
//这是一个典型的按钮触发事件的写法,通过监听事件和在参数里面new一个监听对象的匿名类方式,还有就是通过intent把数据传递到下一个页面。
}
}
LiuBei.java:
package com.example.lengxiaohua.works;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.String;
//子界面,通过前一个界面传输过来的数据,随机生成一个相应的页面。
public class LiuBei extends AppCompatActivity {
private TextView user_name, rows_name; //定义两个用来存储文本对象的变量
private int randomNum; //定义一个随机数变量
private ImageView imageView; //定义用来存储图片对象的变量
private Bitmap bitmap; //用来获取图形资源的变量
private Resources resources; //定义一个资源变量
private static Context mContext; //定义一个上下文变量
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_liubei);
//两个自动生成的函数
user_name = (TextView) findViewById(R.id.user_name);
rows_name = (TextView) findViewById(R.id.rows_name);
imageView = (ImageView) findViewById(R.id.PageImg);
//通过id获取要使用的对象
Intent intent = getIntent();
String age = intent.getStringExtra("userage");
String sex = intent.getStringExtra("usersex");
String data = intent.getStringExtra("username");
//通过intent把数据拿到并赋值给定义的用来存储的变量
mContext = getApplicationContext();
//获得当前的上下文对象
user_name.setText(data);
//把用户输入的名字给替换到页面对应的位置
randomNum = (int) (1 + Math.random() * 5);
//产生一个1-5的随机数
//下面是根据不同的随机数生成不同的页面,用一个switch来选择
if (sex.equals("male")) {
switch (randomNum) {
case 1:
bitmap = getDrableImage(LiuBei.getContext(),"liubei");
//获得drawable下的图片资源
imageView.setImageBitmap(bitmap);
//把背景图片替换为获得的图片资源
rows_name.setText("刘备");
//更改角色对应的名称
break;
case 2:
bitmap = getDrableImage(LiuBei.getContext(),"guanyv");
imageView.setImageBitmap(bitmap);
rows_name.setText("关羽");
break;
case 3:
bitmap = getDrableImage(LiuBei.getContext(),"huangzhong");
imageView.setImageBitmap(bitmap);
rows_name.setText("黄忠");
break;
case 4:
bitmap = getDrableImage(LiuBei.getContext(),"machao");
imageView.setImageBitmap(bitmap);
rows_name.setText("马超");
break;
case 5:
bitmap = getDrableImage(LiuBei.getContext(),"zhaoyun");
imageView.setImageBitmap(bitmap);
rows_name.setText("赵云");
break;
default:
break;
}
} else{
switch (randomNum) {
case 1:
bitmap = getDrableImage(LiuBei.getContext(),"caiwenji");
imageView.setImageBitmap(bitmap);
rows_name.setText("蔡文姬");
break;
case 2:
bitmap = getDrableImage(LiuBei.getContext(),"diaochan");
imageView.setImageBitmap(bitmap);
rows_name.setText("貂蝉");
break;
case 3:
bitmap = getDrableImage(LiuBei.getContext(),"suanshangxiang");
imageView.setImageBitmap(bitmap);
rows_name.setText("孙尚香");
break;
case 4:
bitmap = getDrableImage(LiuBei.getContext(),"xiaoqiao");
imageView.setImageBitmap(bitmap);
rows_name.setText("小乔");
break;
case 5:
bitmap = getDrableImage(LiuBei.getContext(),"zurong");
imageView.setImageBitmap(bitmap);
rows_name.setText("祝融");
break;
default:
break;
}
}
}
//下面是获得图片资源对象的一个封装好的方法之一
public static Bitmap getLoacalBitmap(String url) {
try {
FileInputStream fis = new FileInputStream(url);
return BitmapFactory.decodeStream(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
//下面是获得图片资源对象的一个封装好的方法之二
public static Bitmap getDrableImage(Context context, String name) {
ApplicationInfo info = context.getApplicationInfo();
Resources resources = context.getResources();
int resId = resources.getIdentifier(name, "drawable", info.packageName);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
return BitmapFactory.decodeResource(resources, resId, options);
}
//下面是获得图片URI的一个封装好的方法
public static Uri getImageUri(Context context, String name) {
return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/drawable/" + name);
}
//下面的获得当前对象上下文的一个封装好的方法
public static Context getContext(){
return mContext;
}
}
截图
运行过程
点击运行,选择创建好的虚拟机,我创建的是最高版本的,然后代码是使用最低版本兼容的,所以100%可以允许。
结果截图
登陆界面
选择和输入信息
点击“点击提交”按钮之后弹出的页面
返回主界面之后重新选择年龄段(性别没变)
点击按钮重新进入之后
返回主界面修改性别和名字
点击按钮后产生的页面
返回主界面修改年龄段
点击按钮后生成新页面
结论
结果分析
就是通过简单的随机数生成不同页面,性别用来判断一下要生成人物的性别。主要就是数据从一个页面传到下一个页面还有获取drawable下图片这两点比较有难度而言。
经验总结
在安装Android studio的时候费了很大功夫,大概用来一个星期的时间,百度上的解答也是水平有限。所以在接触一个不懂的新的技术的时候,就只能靠自己不断尝试,不断搜寻与答案相关的信息,仅此而已。
参考文献
《AndroidStudio安装配置教程》:https://blog.youkuaiyun.com/m0_37240709/article/details/76069176
《获取Drawable目录下的资源》:
https://blog.youkuaiyun.com/sinat_34383316/article/details/80927418
《在工具类里获取context 上下文对象 Android》:
https://blog.youkuaiyun.com/beijinghsj/article/details/51952702