当前位置:网站首页>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;
}
}
边栏推荐
- Required request body is missing:(跨域问题)
- web安全与防御
- Insight into cloud native | microservices and microservice architecture
- Chrome browser tag management plug-in – onetab
- Thinkphp5 how to determine whether a table exists
- MySql报错:unblock with mysqladmin flush-hosts
- 企业级SaaS CRM实现
- 双非本科生进大厂,而我还在底层默默地爬树(上)
- From concept to method, the statistical learning method -- Chapter 3, k-nearest neighbor method
- Microservice practice | declarative service invocation openfeign practice
猜你喜欢
概念到方法,绝了《统计学习方法》——第三章、k近邻法
微服务实战|熔断器Hystrix初体验
Knowledge points are very detailed (code is annotated) number structure (C language) -- Chapter 3, stack and queue
微服务实战|原生态实现服务的发现与调用
Probability is not yet. Look at statistical learning methods -- Chapter 4, naive Bayesian method
[staff] common symbols of staff (Hualian clef | treble clef | bass clef | rest | bar line)
以字节跳动内部 Data Catalog 架构升级为例聊业务系统的性能优化
Troubleshooting and handling of an online problem caused by redis zadd
Microservice practice | Eureka registration center and cluster construction
自定义Redis连接池
随机推荐
Enterprise level SaaS CRM implementation
Discussion on improving development quality and reducing test bug rate
Matplotlib剑客行——布局指南与多图实现(更新)
Amq6126 problem solving ideas
Machine learning practice: is Mermaid a love movie or an action movie? KNN announces the answer
Number structure (C language -- code with comments) -- Chapter 2, linear table (updated version)
Chrome browser plug-in fatkun installation and introduction
What are the differences between TP5 and laravel
Thinkphp5 how to determine whether a table exists
《统计学习方法》——第五章、决策树模型与学习(上)
Say goodbye to 996. What are the necessary plug-ins in idea?
Probability is not yet. Look at statistical learning methods -- Chapter 4, naive Bayesian method
Timed thread pool implements request merging
每天睡前30分钟阅读Day6_Day6_Date_Calendar_LocalDate_TimeStamp_LocalTime
Chrome浏览器标签管理插件–OneTab
我服了,MySQL表500W行,居然有人不做分区?
微服务实战|微服务网关Zuul入门与实战
DTM distributed transaction manager PHP collaboration client V0.1 beta release!!!
洞见云原生|微服务及微服务架构浅析
记录下对游戏主机配置的个人理解与心得