当前位置:编程学习 > wap >>

DEMO:滑动星期日历~~~刚完成 上来分享

废话不多说 老板当时要求的效果~

美工没有做,但是功能实现啦 

代码先~

package com.example.test;

import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import android.os.*;
import android.app.*;
import android.view.*;
import android.view.View.*;
import android.view.animation.AnimationUtils;
import android.widget.*;
import android.widget.LinearLayout.LayoutParams;

public class MainActivity extends Activity implements OnClickListener, android.view.GestureDetector.OnGestureListener{
private Button b;
int progress = 0;
public final static int start = 0;
public final static int stop = 1;
private ViewFlipper vf_week;
private GestureDetector gesture;
private String day="";


private int n=0; //周期翻页数
private int head = 0;//每月头
private int tail = 0;//每月尾

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);



vf_week = (ViewFlipper) findViewById(R.id.vf_week);




gesture = new GestureDetector(this);

this.vf_week.addView(getWeekText(n,0));

}




/**
 * 根据当日是星期几定位这周
 * @param n eg.0为本周 1为下周 2为下下周 -1为上周 -2为上上周
 * @param m 设置自定义月份 0为当前月份 
 */
private View getWeekText(int n,int m){

Calendar cal = Calendar.getInstance();
int iyear = cal.get(Calendar.YEAR);
String year =iyear +"";
String month = "";
if(m==0){
month = ""+(cal.get(Calendar.MONTH)+1);
}else{
month = m+"";
}

day = cal.get(Calendar.DATE)+"";

int y=Integer.parseInt(year.substring(1));
int c=Integer.parseInt(year.substring(0, 2));


int d=Integer.parseInt(day);

int week=(y+(y/4)+(c/4)-2*c+(26*(m+1)/10)+d-1)%7;

//计算月份日长 是否闰年
String[] months_big = { "1", "3", "5", "7", "8", "10", "12" };
String[] months_little = { "4", "6", "9", "11" };
final List<String> list_big = Arrays.asList(months_big);
final List<String> list_little = Arrays.asList(months_little);
int monthNum = 31;
if (list_big.contains(month)) {

monthNum = 32;
} else if (list_little.contains(month)) {
monthNum = 31;
} else {
// 闰年
if ((iyear % 4 == 0 && iyear % 100 != 0) || iyear % 400 == 0){
monthNum = 30;
}
else{
monthNum = 29;
}

}

tail = monthNum-1;
//创建view
LinearLayout linear = new LinearLayout(this);
for(int x=0;x<7;x++){
Button b = new Button(this); 
b.setTextSize(15);
        b.setGravity(1);
        b.setBackgroundColor(00000000);
        b.setSingleLine();
       
        b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,1));
        final int temp = d-week+x+7*n;
        head = temp;
        if(temp>0&&temp<monthNum){
         b.setText(temp+"");
        
        }
        
 b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

Toast.makeText(MainActivity.this,temp+"--"+head, 3000).show();
}
});
        
        linear.addView(b);
}
        
        return linear; 

}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub

if(head>tail-1){
Toast.makeText(MainActivity.this, "如需查询下月数据,请选择月份",3000).show();
}else{
if(e1.getX()-e2.getX()>120){
this.vf_week.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
this.vf_week.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
this.vf_week.addView(getWeekText(++n,0), new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 

this.vf_week.showNext();
System.out.println("N:::::::::"+n);
return true;
}
}
 

if(head<8){
Toast.makeText(MainActivity.this, "如需查询上月数据,请选择月份",3000).show();
}else{
if(e1.getX()-e2.getX()<-120){
this.vf_week.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));
this.vf_week.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));
this.vf_week.addView(getWeekText(--n,0), new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 

this.vf_week.showNext();
// n--;
System.out.println("N:::::::::"+n);
return true;
}
}

return true;
}



////===================================================================================================================
//===================================================================================================================
//===================================================================================================================
//===================================================================================================================
//===================================================================================================================



@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return this.gesture.onTouchEvent(event);
}

@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}


@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub

}


@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}


@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
// TODO Auto-generated method stub
return false;
}


@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub

}




@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}





}

//配置文件:::

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >


    <TextView
        android:id="@+id/textView11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/hello_world" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView11" >

        <TextView
            android:id="@+id/textView7"
            style="@style/week_style"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="周日" />

        <TextView
            android:id="@+id/textView6"
            style="@style/week_style"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="周一" />

        <TextView
            android:id="@+id/textView5"
            style="@style/week_style"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="周二" />

        <TextView
            android:id="@+id/textView4"
            style="@style/week_style"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="周三" />

        <TextView
            android:id="@+id/textView3"
            style="@style/week_style"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="周四" />

        <TextView
            android:id="@+id/textView2"
            style="@style/week_style"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="周五" />

        <TextView
            android:id="@+id/textView1"
            style="@style/week_style"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="周六" />
    </LinearLayout>

    <ViewFlipper
        android:id="@+id/vf_week"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/linearLayout1" >

    </ViewFlipper>

</RelativeLayout>


//动画文件:
//left in
<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android">  
    <translate android:fromXDelta="100%p" android:toXDelta="0"  
        android:duration="1000" />  
    <alpha android:fromAlpha="0.1" android:toAlpha="1.0"  
        android:duration="1000" />  
</set>  

//right in
<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android">  
    <translate android:fromXDelta="-100%p" android:toXDelta="0"  
        android:duration="1000" />  
    <alpha android:fromAlpha="0.1" android:toAlpha="1.0"  
        android:duration="1000" />  
</set>  




out就是反一反 字数有限 大家可以自己优化下  有什么不好的地方 随意说 

本人新手 欢迎大家踊跃批评! --------------------编程问答-------------------- 支持下,看起来没大问题 --------------------编程问答-------------------- 这个界面不错 赞 --------------------编程问答--------------------




更新的新手求教 --------------------编程问答-------------------- 求res/values/styles.xml
代码 --------------------编程问答-------------------- anim 我有没有建错 求修改 或者在给代码 谢谢啦 --------------------编程问答-------------------- UI做的不错,发下style和anim文件下的right out和left out的xml看下呗,,, --------------------编程问答-------------------- 还是你想说out和in的值都是反的,,,
--------------------编程问答-------------------- 原来只给了这块的代码,求整个页面的xml,不给activity的代码也行,只想看看整个UI是怎么写的,好心人一生平安,,,
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,