我有7个名为' Sunday'星期六'在我的应用程序中现在我希望我的应用程序根据当天自动选择选项卡,即如果今天是星期五,应该在我的应用程序运行时自动选择星期五选项卡。我怎样才能做到这一点。请帮忙。
MainActivity.java
package com.example.kiran.herau;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity{
FloatingActionButton fab_plus, fab_facebook, fab_gmail;
Animation FabOpen, FabClose, FabRClockwise, FabRAnticlockwise;
boolean isOpen = false;
DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
*Setup the DrawerLayout and NavigationView
*/
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;
/**
* Lets inflate the very first fragment
* Here , we are inflating the TabFragment as the first Fragment
*/
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
/**
* Setup click events on the Navigation View Items.
*/
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
mDrawerLayout.closeDrawers();
if (menuItem.getItemId() == R.id.nav_addschedule) {
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView,new SentFragment()).commit();
}
if (menuItem.getItemId() == R.id.nav_smsschedule) {
// Button b = (Button) findViewById(R.id.smsschedule);
// b.setOnClickListener(new View.OnClickListener(){
//
//
// @Override
// public void onClick(View v) {
// FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
// xfragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
startActivity(new Intent(MainActivity.this,LayoutPrimary.class));
//
// }
// });
}
if (menuItem.getItemId() == R.id.nav_settings) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
}
if (menuItem.getItemId() == R.id.nav_aboutus) {
startActivity(new Intent(MainActivity.this,pop.class)); }
if (menuItem.getItemId() == R.id.nav_share) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
}
return false;
}
});
/**
* Setup Drawer Toggle of the Toolbar
*/
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name,
R.string.app_name);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
fab_plus = (FloatingActionButton)findViewById(R.id.fab_plus);
fab_facebook=(FloatingActionButton)findViewById(R.id.fab_facebook);
fab_gmail=(FloatingActionButton)findViewById(R.id.fab_gmail);
FabOpen= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fab_open);
FabClose= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fab_close);
FabRClockwise= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotateclockwise);
FabRAnticlockwise= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotate_anticlockwise);
button = (Button)findViewById(R.id.secondactivity);
fab_plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isOpen) {
fab_facebook.startAnimation(FabClose);
fab_gmail.startAnimation(FabClose);
fab_plus.startAnimation(FabRAnticlockwise);
fab_facebook.setClickable(false);
fab_gmail.setClickable(false);
isOpen = false;
}
else {
fab_facebook.startAnimation(FabOpen);
fab_gmail.startAnimation(FabOpen);
fab_plus.startAnimation(FabRClockwise);
fab_facebook.setClickable(true);
fab_gmail.setClickable(true);
isOpen = true;
}
}
});
fab_facebook.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent (MainActivity.this,PopView.class));
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent (MainActivity.this,ForthActivity.class));
}
});
fab_gmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent (MainActivity.this,PopAdd.class));
}
});
//
//
// fab_gmail.setOnClickListener(new View.OnClickListener() {
//
//
// @Override
// public void onClick(View v) {
// startActivity(new Intent (MainActivity.this,PopAdd.class));
//
// }
// });
// OnClickButtonListener();
}
//
// public void OnClickButtonListener() {
// fab_gmail.setOnClickListener(new View.OnClickListener() {
//s
//
// @Override
// public void onClick(View v) {
// Intent intent = new Intent("com.example.kiran.herau.SecondActivity");
// startActivity(intent);
//
//
// }
// });
}
TabFragment.java
package com.example.kiran.herau;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import layout.PrimaryFragment;
import layout.SocialFragment;
import layout.UpdatesFragment;
public class TabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 7 ;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**
*Inflate tab_layout and setup Views.
*/
View x = inflater.inflate(R.layout.tab_layout,null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
/**
*Set an Apater for the View Pager
*/
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
/**
* Now , this is a workaround ,
* The setupWithViewPager dose't works without the runnable .
* Maybe a Support Library Bug .
*/
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return x;
}
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
}
/**
* Return fragment with respect to Position .
*/
@Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new PrimaryFragment();
case 1 : return new SocialFragment();
case 2 : return new UpdatesFragment();
case 3 : return new PrimaryFragment();
case 4 : return new PrimaryFragment();
case 5 : return new UpdatesFragment();
case 6 : return new UpdatesFragment();
}
return null;
}
@Override
public int getCount() {
return int_items;
}
/**
* This method returns the title of the tab according to the position.
*/
@Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
return "S";
case 1 :
return "M";
case 2 :
return "T";
case 3 :
return "W";
case 4 :
return "T";
case 5 :
return "F";
case 6 :
return "S";
}
return null;
}
}
}
答案 0 :(得分:0)
package events.chitva.com.events.CalendarLib;
import java.util.Calendar;
/**
* @author Amir
* @author ebraminio
*/
public class GregorianDate extends AbstractDate {
private static final int[] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31,
31, 30, 31, 30, 31};
private int year;
private int month;
private int day;
public GregorianDate() {
this(Calendar.getInstance());
}
public GregorianDate(Calendar calendar) {
this.year = calendar.get(Calendar.YEAR);
this.month = calendar.get(Calendar.MONTH) + 1;
this.day = calendar.get(Calendar.DAY_OF_MONTH);
}
public GregorianDate(int year, int month, int day) {
this();
setYear(year);
setMonth(month);
setDayOfMonth(day);
}
public int getDayOfMonth() {
return day;
}
public void setDayOfMonth(int day) {
if (day < 1)
throw new DayOutOfRangeException("day " + day + " is out of range!");
if (month != 2 && day > daysInMonth[month])
throw new DayOutOfRangeException("day " + day + " is out of range!");
if (month == 2 && isLeapYear() && day > 29)
throw new DayOutOfRangeException("day " + day + " is out of range!");
if (month == 2 && (!isLeapYear()) && day > 28)
throw new DayOutOfRangeException("day " + day + " is out of range!");
// TODO check for the case of leap year for February
this.day = day;
}
public int getDayOfWeek() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DAY_OF_MONTH, day);
return cal.get(Calendar.DAY_OF_WEEK);
}
public int getDayOfYear() {
throw new RuntimeException("not implemented yet!");
}
public String getEvent() {
throw new RuntimeException("not implemented yet!");
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
if (month < 1 || month > 12)
throw new MonthOutOfRangeException("month " + month
+ " is out of range!");
// Set the day again, so that exceptions are thrown if the
// day is out of range
setDayOfMonth(getDayOfMonth());
this.month = month;
}
public int getWeekOfMonth() {
throw new RuntimeException("not implemented yet!");
}
public int getWeekOfYear() {
throw new RuntimeException("not implemented yet!");
}
public int getYear() {
return year;
}
public void setYear(int year) {
if (year == 0)
throw new YearOutOfRangeException("Year 0 is invalid!");
this.year = year;
}
public boolean isLeapYear() {
if (year % 400 == 0)
return true;
else if (year % 100 == 0)
return false;
return (year % 4 == 0);
}
public void rollDay(int amount, boolean up) {
throw new RuntimeException("not implemented yet!");
}
public void rollMonth(int amount, boolean up) {
throw new RuntimeException("not implemented yet!");
}
public void rollYear(int amount, boolean up) {
throw new RuntimeException("not implemented yet!");
}
public boolean equals(GregorianDate gregorianDate) {
return this.getDayOfMonth() == gregorianDate.getDayOfMonth()
&& this.getMonth() == gregorianDate.getMonth()
&& this.getYear() == gregorianDate.getYear();
}
@Override
public GregorianDate clone() {
return new GregorianDate(getYear(), getMonth(), getDayOfMonth());
}
}
使用此库。然后打电话
Date d = new Date();
new GregorianDate(1900 + d.getYear(), d.getMonth() + 1, d.getDate()).getDayOfWeek();
希望能帮到你。
答案 1 :(得分:0)
您可以使用getTabAt(int)在与当天相对应的位置获取setTimeout(function(){document.getElementById('admin').className = 'largetile'; }, menuDelayTimeout);
,并使用select()选择它。
如果您在活动的Tablayout.Tab
或片段的onCreate
中执行此操作,则会在启动时自动选择右侧标签。
onCreateView