当前位置:网站首页>Fragmenttabhost implements the interface of housing loan calculator
Fragmenttabhost implements the interface of housing loan calculator
2022-07-02 09:32:00 【FF small confused acridine~】
Mission requirements :
All classes need to be submitted java Source file code and layout file code (xml file ), Be careful : Submit java Select the code language as Java, When submitting the layout file code, select the code language as XML
Task description :
Use FragmentTabHost Complete the following mortgage calculator program interface , The effect of each tab page is shown in the following figure , requirement :
(1) After the program starts, the first tab page is selected by default ;
(2) After selecting a label page, the pictures and text in the corresponding tab will change color ;
(3) Click the calculation button in the provident fund loan interface , stay Logcat The window displays the loan amount entered by the user .
Link to the image download address required in the interface :https://pan.baidu.com/s/1mSnakdSPvJIPZZS-uWkQHg Extraction code :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"?>
<!-- Layout file of option content page According to the title, you can know that the number you need to input is So it's setting EditText when , Set to pop up the numeric keypad -->
<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=" Loan amount ( Ten thousand yuan )" 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=" The term of the loan ( year )" 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=" lending rate (%)" 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=" Calculation " 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"> Mortgage calculator </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);
// obtain FragmentTabHost References to
FragmentTabHost fragmentTabHost = findViewById(android.R.id.tabhost);
// initialization
fragmentTabHost.setup(this,
getSupportFragmentManager(),// Manage multiple Fragment object
android.R.id.tabcontent);// Display the control of the content page id
// Create content pages TabSpec object
TabHost.TabSpec tab1 = fragmentTabHost.newTabSpec("first_tab")
.setIndicator(getTabSpecView("first_tab"," Provident fund loans ",R.drawable.first));
fragmentTabHost.addTab(tab1, ProvidentFragment.class, null );// Use... When passing data , When there is no need to transmit data, it is directly transmitted null
TabHost.TabSpec tab2 = fragmentTabHost.newTabSpec("second_tab")
.setIndicator(getTabSpecView("second_tab"," Commercial loans ",R.drawable.second));
fragmentTabHost.addTab(tab2, BusinessFragment.class,null);
TabHost.TabSpec tab3 = fragmentTabHost.newTabSpec("third_tab")
.setIndicator(getTabSpecView("third_tab"," Portfolio loans ",R.drawable.third));
fragmentTabHost.addTab(tab3, CombinationFragment.class,null);
// Handle fragmentTabHost Option switching event
fragmentTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
// Change the color of pictures and text
switch (tabId){
case "first_tab":// Provident fund loan
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":// Selected commercial loans
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":// Portfolio loan selected
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;
}
}
});
// Set the tab selected by default : The parameter is subscript
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);
// obtain tab_spec_layout Reference of view control in layout
ImageView icon = view.findViewById(R.id.icon);
icon.setImageResource(drawable);
// take ImageView Object store to Map in
imageViewMap.put(tag,icon);
TextView tvTitle = view.findViewById(R.id.title);
tvTitle.setText(title);
// take TextView Object store to Map in
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) {
// Load the layout file of the content page ( Put the... Of the content page XML The layout file is converted to View Object of type )
View view = inflater.inflate(R.layout.fragment_layout,// Layout file of content page
container,// Root view object
false );//false Indicates that you need to call addView Methods will view Add to container
//true Indicates that you do not need to call addView Method
// Get the reference of the control in the content
TextView tvContent = view.findViewById(R.id.tv_content);
tvContent.setText(" Provident fund loans ");
Button btnCount = view.findViewById(R.id.btn_count);
final EditText etMoney = view.findViewById(R.id.et_money);
// Set the listening event of the button
btnCount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str = "";
str = etMoney.getText().toString();
Log.e(" Provident fund loan amount ( Ten thousand yuan )",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) {
// Load the layout file of the content page ( Put the... Of the content page XML The layout file is converted to View Object of type )
View view = inflater.inflate(R.layout.fragment_layout,// Layout file of content page
container,// Root view object
false );//false Indicates that you need to call addView Methods will view Add to container
//true Indicates that you do not need to call addView Method
// Get the reference of the control in the content
TextView tvContent = view.findViewById(R.id.tv_content);
tvContent.setText(" Commercial loans ");
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) {
// Load the layout file of the content page ( Put the... Of the content page XML The layout file is converted to View Object of type )
View view = inflater.inflate(R.layout.fragment_layout,// Layout file of content page
container,// Root view object
false );//false Indicates that you need to call addView Methods will view Add to container
//true Indicates that you do not need to call addView Method
// Get the reference of the control in the content
TextView tvContent = view.findViewById(R.id.tv_content);
tvContent.setText(" Portfolio loans ");
return view;
}
}
边栏推荐
- Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedd
- 每天睡前30分钟阅读Day6_Day6_Date_Calendar_LocalDate_TimeStamp_LocalTime
- 数构(C语言)——第四章、矩阵的压缩存储(下)
- 微服务实战|熔断器Hystrix初体验
- C语言之分草莓
- [staff] common symbols of staff (Hualian clef | treble clef | bass clef | rest | bar line)
- 十年開發經驗的程序員告訴你,你還缺少哪些核心競爭力?
- In depth analysis of how the JVM executes Hello World
- Pool de connexion redis personnalisé
- 破茧|一文说透什么是真正的云原生
猜你喜欢

JDBC回顾

Pool de connexion redis personnalisé

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

Matplotlib swordsman - a stylist who can draw without tools and code

破茧|一文说透什么是真正的云原生

Chrome浏览器插件-Fatkun安装和介绍

Chrome video download Plug-in – video downloader for Chrome

Programmers with ten years of development experience tell you, what core competitiveness do you lack?

概念到方法,绝了《统计学习方法》——第三章、k近邻法

洞见云原生|微服务及微服务架构浅析
随机推荐
Cartoon rendering - average normal stroke
I've taken it. MySQL table 500W rows, but someone doesn't partition it?
Required request body is missing:(跨域问题)
From concept to method, the statistical learning method -- Chapter 3, k-nearest neighbor method
Chrome video download Plug-in – video downloader for Chrome
[go practical basis] how to verify request parameters in gin
西瓜书--第六章.支持向量机(SVM)
How to install PHP in CentOS
记录下对游戏主机配置的个人理解与心得
数构(C语言--代码有注释)——第二章、线性表(更新版)
Knife4j 2.X版本文件上传无选择文件控件问题解决
VIM操作命令大全
MySQL事务
[go practical basis] how to set the route in gin
Attributes of classfile
FragmentTabHost实现房贷计算器界面
Hystrix implements request consolidation
[staff] time mark and note duration (staff time mark | full note rest | half note rest | quarter note rest | eighth note rest | sixteenth note rest | thirty second note rest)
Double non undergraduate students enter the factory, while I am still quietly climbing trees at the bottom (Part 1)
Dix ans d'expérience dans le développement de programmeurs vous disent quelles compétences de base vous manquez encore?