It's simple, it's complicated, so it's easy to organize it for later use
The code is a bit messy, hahaha, does not affect the use
Tool code:
package cn.statistic.cong.monthquarteryearstatisticdemo;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.format.Time;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;
import java.util.ArrayList;
public class SelectDateActivity extends AppCompatActivity implements View.OnClickListener {
private RadioGroup rg_year;
private RadioGroup rg_month;
private RadioGroup rg_jidu;
private RadioButton radioButtonNowYear;
private RadioButton radioButtonNowMonth;
private RadioButton radioButtonNowJidu;
private String nowjidu;
private TextView tv_show;
private TextView tv_sure;
private ArrayList<String> arrayListjidu = new ArrayList<>();
private PopupWindow popupwindow;
private ScrollView sv_yuedu;
private ScrollView sv_jidu;
private ImageView iv_show_pop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_select_date);
arrayListjidu.add("第一季度");
arrayListjidu.add("第二季度");
arrayListjidu.add("第三季度");
arrayListjidu.add("第四季度");
initView();
}
private void initView() {
rg_year = (RadioGroup) findViewById(R.id.rg_year);
rg_month = (RadioGroup) findViewById(R.id.rg_month);
rg_jidu = (RadioGroup) findViewById(R.id.rg_jidu);
tv_show = (TextView) findViewById(R.id.tv_show);
tv_sure = (TextView) findViewById(R.id.tv_sure);
sv_yuedu = (ScrollView) findViewById(R.id.sv_yuedu);
sv_jidu = (ScrollView) findViewById(R.id.sv_jidu);
iv_show_pop = (ImageView) findViewById(R.id.iv_show_pop);
tv_sure.setOnClickListener(this);
final Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
t.setToNow(); // 取得系统时间。
final int years = t.year;
final int yearselect = t.year;
final int monthselect = t.month+1;
if ((t.month+1)>=1&&(t.month+1)<=3){
nowjidu = "第一季度";
}else if ((t.month+1)>=4&&(t.month+1)<=6){
nowjidu = "第二季度";
}else if ((t.month+1)>=7&&(t.month+1)<=9){
nowjidu = "第三季度";
}else if ((t.month+1)>=10&&(t.month+1)<=12){
nowjidu = "第四季度";
}
for (int i = years; i > years - 5 ; i--) {
RadioButton radioButton = new RadioButton(this);
RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT);
lp.setMargins(2,0,2,2);
radioButton.setLayoutParams(lp);
radioButton.setBackgroundResource(R.drawable.date_select_selector);
radioButton.setPadding(15, 15, 15, 15);
radioButton.setButtonDrawable(android.R.color.transparent);//隐藏单选圆形按钮
radioButton.setGravity(Gravity.CENTER);
radioButton.setText(i+"");
radioButton.setTextColor(getResources().getColorStateList(R.color.tab_host_txt_selector));
rg_year.addView(radioButton);
if (i == t.year){
radioButton.setChecked(true);
}
}
for (int i = 12; i > 0; i--) {
RadioButton radioButton = new RadioButton(this);
RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT);
lp.setMargins(0,0,0,2);
radioButton.setLayoutParams(lp);
radioButton.setButtonDrawable(android.R.color.transparent);//隐藏单选圆形按钮
radioButton.setGravity(Gravity.CENTER);
radioButton.setBackgroundResource(R.drawable.date_select_selector);
radioButton.setPadding(15, 15, 15, 15);
radioButton.setText(i+"月");
radioButton.setTextColor(getResources().getColorStateList(R.color.tab_host_txt_selector));
rg_month.addView(radioButton);
if (i == t.month+1){
radioButton.setChecked(true);
}
}
for (int i = 0; i < arrayListjidu.size(); i++) {
RadioButton radioButton = new RadioButton(this);
RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT);
lp.setMargins(0,0,0,2);
radioButton.setLayoutParams(lp);
radioButton.setButtonDrawable(android.R.color.transparent);//隐藏单选圆形按钮
radioButton.setGravity(Gravity.CENTER);
radioButton.setBackgroundResource(R.drawable.date_select_selector);
radioButton.setPadding(15, 15, 15, 15);
radioButton.setText(arrayListjidu.get(i)+"");
radioButton.setTextColor(getResources().getColorStateList(R.color.tab_host_txt_selector));
rg_jidu.addView(radioButton);
if (arrayListjidu.get(i).equals(nowjidu)){
radioButton.setChecked(true);
}
}
rg_year.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
RadioButton radioButton = (RadioButton) findViewById(checkedId);
if ((!radioButton.getText().toString().equals(yearselect))&&radioButtonNowYear!=null){
radioButtonNowYear.setChecked(false);
}
radioButtonNowYear = (RadioButton) findViewById(checkedId);
}
});
rg_month.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
RadioButton radioButton = (RadioButton) findViewById(checkedId);
if ((!radioButton.getText().toString().equals(monthselect))&&radioButtonNowMonth!=null){
radioButtonNowMonth.setChecked(false);
}
radioButtonNowMonth = (RadioButton) findViewById(checkedId);
}
});
rg_jidu.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
RadioButton radioButton = (RadioButton) findViewById(checkedId);
if ((!radioButton.getText().toString().equals(nowjidu))&&radioButtonNowJidu!=null){
radioButtonNowJidu.setChecked(false);
}
radioButtonNowJidu = (RadioButton) findViewById(checkedId);
}
});
}
public void backDate(View view){
finish();
}
public void toSelect(View view){
if (popupwindow!=null&&popupwindow.isShowing()){
popupwindow.dismiss();
iv_show_pop.setImageResource(R.mipmap.arrow_blue_down);
}else{
initPopupWindow();
iv_show_pop.setImageResource(R.mipmap.arrow_blue_up);
}
}
public void initPopupWindow(){
View customView = getLayoutInflater().inflate(R.layout.popupwindow_year_month_jidu,
null, false);
TextView tv_by_year = (TextView) customView.findViewById(R.id.tv_by_year);
TextView tv_by_month = (TextView) customView.findViewById(R.id.tv_by_month);
TextView tv_by_jidu = (TextView) customView.findViewById(R.id.tv_by_jidu);
tv_by_year.setOnClickListener(this);
tv_by_month.setOnClickListener(this);
tv_by_jidu.setOnClickListener(this);
// 创建PopupWindow实例,200,150分别是宽度和高度
DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
popupwindow = new PopupWindow(customView, DrawerLayout.LayoutParams.WRAP_CONTENT,
DrawerLayout.LayoutParams.WRAP_CONTENT);
popupwindow.showAsDropDown(tv_show);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.tv_by_year:
iv_show_pop.setImageResource(R.mipmap.arrow_blue_down);
popupwindow.dismiss();
tv_show.setText("按年选择");
sv_yuedu.setVisibility(View.GONE);
sv_jidu.setVisibility(View.GONE);
break;
case R.id.tv_by_month:
iv_show_pop.setImageResource(R.mipmap.arrow_blue_down);
popupwindow.dismiss();
tv_show.setText("按月选择");
sv_yuedu.setVisibility(View.VISIBLE);
sv_jidu.setVisibility(View.GONE);
break;
case R.id.tv_by_jidu:
iv_show_pop.setImageResource(R.mipmap.arrow_blue_down);
popupwindow.dismiss();
tv_show.setText("按季选择");
sv_yuedu.setVisibility(View.GONE);
sv_jidu.setVisibility(View.VISIBLE);
break;
case R.id.tv_sure:
Intent intent;
if (tv_show.getText().toString().contains("年")){
RadioButton radioButton = (RadioButton) findViewById(rg_year.getCheckedRadioButtonId());
intent = new Intent();
intent.putExtra("type","全年");
intent.putExtra("年",radioButton.getText().toString());
intent.putExtra("月","0");
setResult(1,intent);
finish();
}else if (tv_show.getText().toString().contains("季")){
RadioButton radioButton = (RadioButton) findViewById(rg_year.getCheckedRadioButtonId());
RadioButton radioButton1 = (RadioButton) findViewById(rg_jidu.getCheckedRadioButtonId());
intent = new Intent();
intent.putExtra("type","季度");
intent.putExtra("年",radioButton.getText().toString());
intent.putExtra("月",radioButton1.getText().toString());
setResult(1,intent);
finish();
}else{
RadioButton radioButton = (RadioButton) findViewById(rg_year.getCheckedRadioButtonId());
RadioButton radioButton1 = (RadioButton) findViewById(rg_month.getCheckedRadioButtonId());
intent = new Intent();
intent.putExtra("type","月");
intent.putExtra("年",radioButton.getText().toString());
intent.putExtra("月",radioButton1.getText().toString());
setResult(1,intent);
finish();
}
break;
}
}
}
Tool class xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@mipmap/date_popupwindow_bg"
android:layout_marginLeft="10dp">
<TextView
android:id="@+id/tv_by_year"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="按年选择"
android:textColor="#000"
android:paddingTop="10dp"
android:gravity="center"/>
<TextView
android:id="@+id/tv_by_month"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="按月选择"
android:textColor="#000"
android:layout_weight="1"
android:gravity="center"/>
<TextView
android:id="@+id/tv_by_jidu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="按季选择"
android:textColor="#000"
android:layout_weight="1"
android:gravity="center"/>
</LinearLayout>
Activity code:
package cn.statistic.cong.monthquarteryearstatisticdemo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private HashMap<String,Integer> hashMapJidu= new HashMap<>();
private HashMap<Integer,String> hashMapJidu1= new HashMap<>();
private int years;
private int months;
private int tempYear;
private int tempYearMonth;
private int tempYearMonth1;
private int tempYearJidu;
private int tempYearJidu1;
private TextView tv_select_date;
private TextView tv_xinashi_type;
private ImageView jian_select_date;
private ImageView jia_select_date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
hashMapJidu.put("第一季度",1);
hashMapJidu.put("第二季度",2);
hashMapJidu.put("第三季度",3);
hashMapJidu.put("第四季度",4);
hashMapJidu1.put(1,"第一季度");
hashMapJidu1.put(2,"第二季度");
hashMapJidu1.put(3,"第三季度");
hashMapJidu1.put(4,"第四季度");
tv_select_date = (TextView) findViewById(R.id.tv_select_date);
tv_xinashi_type = (TextView) findViewById(R.id.tv_xinashi_type);
jian_select_date = (ImageView) findViewById(R.id.jian_select_date);
jia_select_date = (ImageView) findViewById(R.id.jia_select_date);
final Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
t.setToNow(); // 取得系统时间。
years = t.year;
months = t.month+1;
tv_select_date.setText(years+"-"+months);
tv_select_date.setOnClickListener(this);
jian_select_date.setOnClickListener(this);
jia_select_date.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.tv_select_date:
Intent injiayou = new Intent(MainActivity.this, SelectDateActivity.class);
startActivityForResult(injiayou,1);
break;
case R.id.jian_select_date:
if (tv_xinashi_type.getText().toString().contains("年")){
tempYear = Integer.parseInt(tv_select_date.getText().toString().replaceAll("年",""));
if (tempYear>(years-5)){
tempYear--;
tv_select_date.setText(tempYear+"");
}else{
}
}else if (tv_xinashi_type.getText().toString().contains("月")){
tempYearMonth = Integer.parseInt(tv_select_date.getText().toString().split("-")[1]);//月
tempYearMonth1 = Integer.parseInt(tv_select_date.getText().toString().split("-")[0]);//年
if (tempYearMonth>1){
tempYearMonth--;
tv_select_date.setText(tempYearMonth1+"-"+tempYearMonth);
}else{
if (tempYearMonth1>years-5){
tempYearMonth1--;
tempYearMonth = 12;
tv_select_date.setText(tempYearMonth1+"-"+tempYearMonth);
}
}
}else if (tv_xinashi_type.getText().toString().contains("季")){
Log.e("zcc",tv_select_date.getText().toString().split("年")[0]+"---"+tv_select_date.getText().toString().split("年")[1]);
tempYearJidu = Integer.parseInt(tv_select_date.getText().toString().split("年")[0]);//年
tempYearJidu1 = hashMapJidu.get(tv_select_date.getText().toString().split("年")[1]);//季度对应的数字
if (tempYearJidu1>1){
tempYearJidu1--;
tv_select_date.setText(tempYearJidu+"年"+hashMapJidu1.get(tempYearJidu1));
}else{
if (tempYearJidu>years-5){
tempYearJidu--;
tv_select_date.setText(tempYearJidu+"年"+"第四季度");
}
}
}
break;
case R.id.jia_select_date:
if (tv_xinashi_type.getText().toString().contains("年")){
tempYear = Integer.parseInt(tv_select_date.getText().toString().replaceAll("年",""));
if (tempYear<years){
tempYear++;
tv_select_date.setText(tempYear+"");
}
}else if (tv_xinashi_type.getText().toString().contains("月")){
tempYearMonth = Integer.parseInt(tv_select_date.getText().toString().split("-")[1]);//月
tempYearMonth1 = Integer.parseInt(tv_select_date.getText().toString().split("-")[0]);//年
if (tempYearMonth<12){
tempYearMonth++;
tv_select_date.setText(tempYearMonth1+"-"+tempYearMonth);
}else{
if (tempYearMonth1<years){
tempYearMonth1++;
tempYearMonth = 1;
tv_select_date.setText(tempYearMonth1+"-"+tempYearMonth);
}
}
}else if (tv_xinashi_type.getText().toString().contains("季")){
tempYearJidu = Integer.parseInt(tv_select_date.getText().toString().split("年")[0]);//年
tempYearJidu1 = hashMapJidu.get(tv_select_date.getText().toString().split("年")[1]);//季度对应的数字
if (tempYearJidu1<4){
tempYearJidu1++;
tv_select_date.setText(tempYearJidu+"年"+hashMapJidu1.get(tempYearJidu1));
}else{
if (tempYearJidu<years){
tempYearJidu++;
tv_select_date.setText(tempYearJidu+"年"+"第一季度");
}
}
}
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1&& resultCode == 1){
String type = data.getStringExtra("type");
String year = data.getStringExtra("年");
String month = data.getStringExtra("月").replace("月","");
if (type.contains("年")){
tv_xinashi_type.setText("年度统计");
tv_select_date.setText(year);
}else if (type.contains("月")){
tv_select_date.setText(year+"-"+month);
tv_xinashi_type.setText("月度统计");
}else if (type.contains("季")){
tv_xinashi_type.setText("季度统计");
tv_select_date.setText(year+"年"+month);
}
}
}
}
xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FAFAFA">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FAFAFA"
android:padding="10dp">
<TextView
android:id="@+id/tv_xinashi_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="月度统计"
android:textColor="#000"
android:textSize="16sp"
android:layout_centerVertical="true"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerInParent="true"
android:gravity="center_vertical">
<ImageView
android:id="@+id/jian_select_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/arrow_blue_lift"
android:padding="4dp"/>
<TextView
android:id="@+id/tv_select_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="16sp"
android:textColor="#0095CF"/>
<ImageView
android:id="@+id/jia_select_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/arrow_blue_right"
android:padding="4dp"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
demo site: https://download.csdn.net/download/congcongguniang/10676909