当前位置:网站首页>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;
}
}
边栏推荐
- C语言之到底是不是太胖了
- 别找了,Chrome浏览器必装插件都在这了
- 微服务实战|负载均衡组件及源码分析
- Thinkphp5 how to determine whether a table exists
- 我服了,MySQL表500W行,居然有人不做分区?
- How to choose between efficiency and correctness of these three implementation methods of distributed locks?
- 一篇详解带你再次重现《统计学习方法》——第二章、感知机模型
- JVM instruction mnemonic
- AMQ 4043 solution for errors when using IBM MQ remote connection
- 知识点很细(代码有注释)数构(C语言)——第三章、栈和队列
猜你喜欢
微服务实战|熔断器Hystrix初体验
Operation and application of stack and queue
MySQL multi column in operation
Probability is not yet. Look at statistical learning methods -- Chapter 4, naive Bayesian method
Oracle modify database character set
Cloudreve自建云盘实践,我说了没人能限制得了我的容量和速度
The channel cannot be viewed when the queue manager is running
以字节跳动内部 Data Catalog 架构升级为例聊业务系统的性能优化
别找了,Chrome浏览器必装插件都在这了
Microservice practice | fuse hytrix initial experience
随机推荐
微服务实战|声明式服务调用OpenFeign实践
MySQL事务
[go practical basis] how to set the route in gin
[staff] time sign and note duration (full note | half note | quarter note | eighth note | sixteenth note | thirty second note)
机器学习实战:《美人鱼》属于爱情片还是动作片?KNN揭晓答案
Number structure (C language) -- Chapter 4, compressed storage of matrices (Part 2)
VIM operation command Encyclopedia
Jd.com interviewer asked: what is the difference between using on or where in the left join association table and conditions
Cloudrev self built cloud disk practice, I said that no one can limit my capacity and speed
Matplotlib剑客行——容纳百川的艺术家教程
What are the differences between TP5 and laravel
Chrome用户脚本管理器-Tampermonkey 油猴
C语言之到底是不是太胖了
Thinkphp5 how to determine whether a table exists
From concept to method, the statistical learning method -- Chapter 3, k-nearest neighbor method
Ora-12514 problem solving method
Number structure (C language -- code with comments) -- Chapter 2, linear table (updated version)
Chrome user script manager tempermonkey monkey
[go practical basis] how can gin get the request parameters of get and post
Elastic Stack之Beats(Filebeat、Metricbeat)、Kibana、Logstash教程