当前位置:网站首页>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;
}
}
边栏推荐
- Solutions to Chinese garbled code in CMD window
- 深入剖析JVM是如何执行Hello World的
- C语言之到底是不是太胖了
- Watermelon book -- Chapter 5 neural network
- 十年開發經驗的程序員告訴你,你還缺少哪些核心競爭力?
- Matplotlib swordsman Tour - an artist tutorial to accommodate all rivers
- Thinkphp5 how to determine whether a table exists
- Bold prediction: it will become the core player of 5g
- Beats (filebeat, metricbeat), kibana, logstack tutorial of elastic stack
- Talk about the secret of high performance of message queue -- zero copy technology
猜你喜欢
![[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)](/img/7f/2cd789339237b7a881bfed7b7545a9.jpg)
[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)

Mysql默认事务隔离级别及行锁

Matplotlib swordsman Tour - an artist tutorial to accommodate all rivers

十年開發經驗的程序員告訴你,你還缺少哪些核心競爭力?

Timed thread pool implements request merging

Probability is not yet. Look at statistical learning methods -- Chapter 4, naive Bayesian method

Supplier selection and prequalification of Oracle project management system

Elastic Stack之Beats(Filebeat、Metricbeat)、Kibana、Logstash教程

Chrome browser tag management plug-in – onetab

Taking the upgrade of ByteDance internal data catalog architecture as an example, talk about the performance optimization of business system
随机推荐
Don't look for it. All the necessary plug-ins for Chrome browser are here
MySql报错:unblock with mysqladmin flush-hosts
What are the waiting methods of selenium
web安全与防御
JDBC回顾
Required request body is missing:(跨域问题)
Programmers with ten years of development experience tell you, what core competitiveness do you lack?
每天睡前30分钟阅读Day6_Day6_Date_Calendar_LocalDate_TimeStamp_LocalTime
Complete solution of servlet: inheritance relationship, life cycle, container, request forwarding and redirection, etc
Watermelon book -- Chapter 6 Support vector machine (SVM)
十年開發經驗的程序員告訴你,你還缺少哪些核心競爭力?
Microservice practice | Eureka registration center and cluster construction
"Redis source code series" learning and thinking about source code reading
Alibaba /热门json解析开源项目 fastjson2
How to install PHP in CentOS
How to use pyqt5 to make a sensitive word detection tool
概念到方法,绝了《统计学习方法》——第三章、k近邻法
Dix ans d'expérience dans le développement de programmeurs vous disent quelles compétences de base vous manquez encore?
C语言之判断直角三角形
微服务实战|声明式服务调用OpenFeign实践