当前位置:网站首页>FragmentTabHost实现房贷计算器界面
FragmentTabHost实现房贷计算器界面
2022-07-02 06:34:00 【FF小迷糊吖~】
任务要求:
需要提交所有类的java源文件代码和布局文件代码(xml文件),注意:提交java源文件代码时选择代码语言为Java,提交布局文件代码时选择代码语言为XML
任务描述:
使用FragmentTabHost完成如下房贷计算器程序界面,每个标签页面的效果如下图所示,要求:
(1)程序启动后默认选中第一个标签页面;
(2)选中某个标签页面以后相应选项卡中的图片和文字都要变色;
(3)点击公积金贷款界面的计算按钮时,在Logcat窗口显示用户输入的贷款金额。
界面中所需图片下载地址链接:https://pan.baidu.com/s/1mSnakdSPvJIPZZS-uWkQHg 提取码:uqv2


activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent">
<TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true"/>
<FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@android:id/tabs"/>
</RelativeLayout>
</androidx.fragment.app.FragmentTabHost>
fragment_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<!--选项内容页面的布局文件 根据题目可得知需要输入的为数字 因此在设置EditText时,设置为弹出数字键盘-->
<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/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" android:layout_marginTop="15dp" android:layout_gravity="center_horizontal" android:textColor="@color/colorPrimary"/>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="15dp" android:paddingRight="15dp" android:orientation="vertical">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="15dp" android:layout_marginTop="15dp">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="贷款金额(万元)" android:textColor="#757575" android:textSize="20sp"/>
<EditText android:id="@+id/et_money" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="15dp">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="贷款期限 (年)" android:textColor="#757575" android:textSize="20sp"/>
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="20dp">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="贷款利率 (%)" android:textColor="#757575" android:textSize="20sp"/>
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" />
</LinearLayout>
<Button android:id="@+id/btn_count" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="计算" android:layout_gravity="center" android:background="@color/colorPrimary"/>
</LinearLayout>
</LinearLayout>
tab_spec_layout.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" android:gravity="center">
<ImageView android:id="@+id/icon" android:layout_width="40dp" android:layout_height="40dp"/>
<TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
strings.xml:
<resources>
<string name="app_name">房贷计算器</string>
</resources>
MainActivity.java:
package com.example.homework03;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTabHost;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import com.example.homework03.fragment.BusinessFragment;
import com.example.homework03.fragment.CombinationFragment;
import com.example.homework03.fragment.ProvidentFragment;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private Map<String, ImageView> imageViewMap = new HashMap<>();
private Map<String, TextView> textViewMap = new HashMap<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取FragmentTabHost的引用
FragmentTabHost fragmentTabHost = findViewById(android.R.id.tabhost);
//初始化
fragmentTabHost.setup(this,
getSupportFragmentManager(),//管理多个Fragment对象
android.R.id.tabcontent);//显示内容页面的控件的id
//创建内容页面TabSpec对象
TabHost.TabSpec tab1 = fragmentTabHost.newTabSpec("first_tab")
.setIndicator(getTabSpecView("first_tab","公积金贷款",R.drawable.first));
fragmentTabHost.addTab(tab1, ProvidentFragment.class, null );//传递数据时使用,不需要传递数据时直接传null
TabHost.TabSpec tab2 = fragmentTabHost.newTabSpec("second_tab")
.setIndicator(getTabSpecView("second_tab","商业贷款",R.drawable.second));
fragmentTabHost.addTab(tab2, BusinessFragment.class,null);
TabHost.TabSpec tab3 = fragmentTabHost.newTabSpec("third_tab")
.setIndicator(getTabSpecView("third_tab","组合贷款",R.drawable.third));
fragmentTabHost.addTab(tab3, CombinationFragment.class,null);
//处理fragmentTabHost的选项切换事件
fragmentTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
//修改图片和文字的颜色
switch (tabId){
case "first_tab"://选中了公积金贷款
imageViewMap.get("first_tab").setImageResource(R.drawable.first1);
textViewMap.get("first_tab").setTextColor(getResources().getColor(R.color.mycolor));
imageViewMap.get("second_tab").setImageResource(R.drawable.second);
textViewMap.get("second_tab").setTextColor(getResources().getColor(android.R.color.black));
imageViewMap.get("third_tab").setImageResource(R.drawable.third);
textViewMap.get("third_tab").setTextColor(getResources().getColor(android.R.color.black));
break;
case "second_tab"://选中了商业贷款
imageViewMap.get("first_tab").setImageResource(R.drawable.first);
textViewMap.get("first_tab").setTextColor(getResources().getColor(android.R.color.black));
imageViewMap.get("second_tab").setImageResource(R.drawable.second1);
textViewMap.get("second_tab").setTextColor(getResources().getColor(R.color.mycolor));
imageViewMap.get("third_tab").setImageResource(R.drawable.third);
textViewMap.get("third_tab").setTextColor(getResources().getColor(android.R.color.black));
break;
case "third_tab"://选中了组合贷款
imageViewMap.get("first_tab").setImageResource(R.drawable.first);
textViewMap.get("first_tab").setTextColor(getResources().getColor(android.R.color.black));
imageViewMap.get("second_tab").setImageResource(R.drawable.second);
textViewMap.get("second_tab").setTextColor(getResources().getColor(android.R.color.black));
imageViewMap.get("third_tab").setImageResource(R.drawable.third1);
textViewMap.get("third_tab").setTextColor(getResources().getColor(R.color.mycolor));
break;
}
}
});
//设置默认选中的标签页:参数是下标
fragmentTabHost.setCurrentTab(0);
imageViewMap.get("first_tab").setImageResource(R.drawable.first1);
textViewMap.get("first_tab").setTextColor(getResources().getColor(R.color.mycolor));
}
public View getTabSpecView(String tag, String title, int drawable){
View view = getLayoutInflater().inflate(R.layout.tab_spec_layout,null);
//获取tab_spec_layout布局当中视图控件的引用
ImageView icon = view.findViewById(R.id.icon);
icon.setImageResource(drawable);
//将ImageView对象存储到Map中
imageViewMap.put(tag,icon);
TextView tvTitle = view.findViewById(R.id.title);
tvTitle.setText(title);
//将TextView对象存储到Map中
textViewMap.put(tag,tvTitle);
return view;
}
}
ProvidentFragment.java:
package com.example.homework03.fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.homework03.R;
public class ProvidentFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//加载内容页面的布局文件(将内容页面的XML布局文件转成View类型的对象)
View view = inflater.inflate(R.layout.fragment_layout,//内容页面的布局文件
container,//根视图对象
false );//false表示需要手动调用addView方法将view添加到container
//true表示不需要手动调用addView方法
//获取内容也当中控件的引用
TextView tvContent = view.findViewById(R.id.tv_content);
tvContent.setText("公积金贷款");
Button btnCount = view.findViewById(R.id.btn_count);
final EditText etMoney = view.findViewById(R.id.et_money);
//设置按钮的监听事件
btnCount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str = "";
str = etMoney.getText().toString();
Log.e("公积金贷款金额(万元)",str);
}
});
return view;
}
}
BusinessFragment.java:
package com.example.homework03.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.homework03.R;
public class BusinessFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//加载内容页面的布局文件(将内容页面的XML布局文件转成View类型的对象)
View view = inflater.inflate(R.layout.fragment_layout,//内容页面的布局文件
container,//根视图对象
false );//false表示需要手动调用addView方法将view添加到container
//true表示不需要手动调用addView方法
//获取内容也当中控件的引用
TextView tvContent = view.findViewById(R.id.tv_content);
tvContent.setText("商业贷款");
return view;
}
}
CombinationFragment.java:
package com.example.homework03.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.homework03.R;
public class CombinationFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//加载内容页面的布局文件(将内容页面的XML布局文件转成View类型的对象)
View view = inflater.inflate(R.layout.fragment_layout,//内容页面的布局文件
container,//根视图对象
false );//false表示需要手动调用addView方法将view添加到container
//true表示不需要手动调用addView方法
//获取内容也当中控件的引用
TextView tvContent = view.findViewById(R.id.tv_content);
tvContent.setText("组合贷款");
return view;
}
}
边栏推荐
- ORA-12514问题解决方法
- AMQ 4043 solution for errors when using IBM MQ remote connection
- Microservice practice | teach you to develop load balancing components hand in hand
- Long summary (code with comments) number structure (C language) -- Chapter 4, string (Part 1)
- Jingdong senior engineer has developed for ten years and compiled "core technology of 100 million traffic website architecture"
- 京东高级工程师开发十年,编写出:“亿级流量网站架构核心技术”
- Chrome浏览器标签管理插件–OneTab
- 「Redis源码系列」关于源码阅读的学习与思考
- Oracle delete tablespace and user
- 京东面试官问:LEFT JOIN关联表中用ON还是WHERE跟条件有什么区别
猜你喜欢

互联网API接口幂等设计

MySQL multi column in operation

Break the cocoon | one article explains what is the real cloud primordial

机器学习实战:《美人鱼》属于爱情片还是动作片?KNN揭晓答案

Bold prediction: it will become the core player of 5g

From concept to method, the statistical learning method -- Chapter 3, k-nearest neighbor method

Cloud computing in my eyes - PAAS (platform as a service)

告别996,IDEA中必装插件有哪些?

Mysql 多列IN操作

Matplotlib swordsman - a stylist who can draw without tools and code
随机推荐
数构(C语言--代码有注释)——第二章、线性表(更新版)
Amq6126 problem solving ideas
[go practical basis] how to customize and use a middleware in gin
Data type case of machine learning -- using data to distinguish men and women based on Naive Bayesian method
Long summary (code with comments) number structure (C language) -- Chapter 4, string (Part 1)
Flink-使用流批一体API统计单词数量
Flink - use the streaming batch API to count the number of words
idea查看字节码配置
Troubleshooting and handling of an online problem caused by redis zadd
别找了,Chrome浏览器必装插件都在这了
Jingdong senior engineer has developed for ten years and compiled "core technology of 100 million traffic website architecture"
From concept to method, the statistical learning method -- Chapter 3, k-nearest neighbor method
【Go实战基础】如何安装和使用 gin
Matplotlib剑客行——容纳百川的艺术家教程
Insight into cloud native | microservices and microservice architecture
ClassFile - Attributes - Code
微服务实战|手把手教你开发负载均衡组件
一篇详解带你再次重现《统计学习方法》——第二章、感知机模型
Machine learning practice: is Mermaid a love movie or an action movie? KNN announces the answer
「Redis源码系列」关于源码阅读的学习与思考