当前位置:网站首页>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;
}
}
边栏推荐
- DTM distributed transaction manager PHP collaboration client V0.1 beta release!!!
- Mathematics in machine learning -- point estimation (I): basic knowledge
- 数构(C语言)——第四章、矩阵的压缩存储(下)
- Beats (filebeat, metricbeat), kibana, logstack tutorial of elastic stack
- VIM操作命令大全
- Redis sorted set data type API and application scenario analysis
- "Interview high frequency question" is 1.5/5 difficult, and the classic "prefix and + dichotomy" application question
- 微服务实战|微服务网关Zuul入门与实战
- Matplotlib剑客行——布局指南与多图实现(更新)
- 自定义Redis连接池
猜你喜欢
MySQL事务
自定义Redis连接池
[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)
Chrome user script manager tempermonkey monkey
盘点典型错误之TypeError: X() got multiple values for argument ‘Y‘
I've taken it. MySQL table 500W rows, but someone doesn't partition it?
Jd.com interviewer asked: what is the difference between using on or where in the left join association table and conditions
Bold prediction: it will become the core player of 5g
How to install PHP in CentOS
[go practical basis] how to set the route in gin
随机推荐
Data type case of machine learning -- using data to distinguish men and women based on Naive Bayesian method
How to use pyqt5 to make a sensitive word detection tool
微服务实战|手把手教你开发负载均衡组件
Alibaba /热门json解析开源项目 fastjson2
Mysql默认事务隔离级别及行锁
Attributes of classfile
Double non undergraduate students enter the factory, while I am still quietly climbing trees at the bottom (Part 1)
Taking the upgrade of ByteDance internal data catalog architecture as an example, talk about the performance optimization of business system
Watermelon book -- Chapter 5 neural network
破茧|一文说透什么是真正的云原生
MySQL multi column in operation
Troubleshooting and handling of an online problem caused by redis zadd
[go practical basis] how to verify request parameters in gin
别找了,Chrome浏览器必装插件都在这了
"Redis source code series" learning and thinking about source code reading
Mathematics in machine learning -- point estimation (I): basic knowledge
Chrome browser plug-in fatkun installation and introduction
Typora安装包分享
Flink - use the streaming batch API to count the number of words
自定義Redis連接池