当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢

"Redis source code series" learning and thinking about source code reading

Enterprise level SaaS CRM implementation

Microservice practice | Eureka registration center and cluster construction

知识点很细(代码有注释)数构(C语言)——第三章、栈和队列

Microservice practice | declarative service invocation openfeign practice

Hystrix implements request consolidation

微服务实战|声明式服务调用OpenFeign实践

Machine learning practice: is Mermaid a love movie or an action movie? KNN announces the answer
![[staff] time sign and note duration (full note | half note | quarter note | eighth note | sixteenth note | thirty second note)](/img/bf/2b0b9c640bdad2c55293f905a22055.jpg)
[staff] time sign and note duration (full note | half note | quarter note | eighth note | sixteenth note | thirty second note)

自定義Redis連接池
随机推荐
Oracle modify database character set
聊聊消息队列高性能的秘密——零拷贝技术
Programmers with ten years of development experience tell you, what core competitiveness do you lack?
Matplotlib swordsman - a stylist who can draw without tools and code
Elastic Stack之Beats(Filebeat、Metricbeat)、Kibana、Logstash教程
「Redis源码系列」关于源码阅读的学习与思考
[staff] common symbols of staff (Hualian clef | treble clef | bass clef | rest | bar line)
I've taken it. MySQL table 500W rows, but someone doesn't partition it?
Knowledge points are very detailed (code is annotated) number structure (C language) -- Chapter 3, stack and queue
VIM操作命令大全
Matplotlib剑客行——容纳百川的艺术家教程
Safety production early warning system software - Download safety production app software
[staff] the lines and spaces of the staff (the nth line and the nth space in the staff | the plus N line and the plus N space on the staff | the plus N line and the plus N space below the staff | the
Chrome browser plug-in fatkun installation and introduction
Flink-使用流批一体API统计单词数量
Redis zadd导致的一次线上问题排查和处理
Alibaba /热门json解析开源项目 fastjson2
告别996,IDEA中必装插件有哪些?
Servlet全解:继承关系、生命周期、容器和请求转发与重定向等
Jingdong senior engineer has developed for ten years and compiled "core technology of 100 million traffic website architecture"