Andriod 簡(jiǎn)單的“日歷”選擇器

public class MainActivity extends ActionBarActivity {
    
    CalendarView view; 
    private AlertDialog.Builder builder;
    private AlertDialog dialog;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button) findViewById(R.id.bt_1);
        button.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                builder = new Builder(MainActivity.this,
                        AlertDialog.THEME_HOLO_DARK);
                final LayoutInflater inflater = LayoutInflater
                        .from(MainActivity.this);
                
                LinearLayout.LayoutParams relalin = new LinearLayout.LayoutParams(
                        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
                LinearLayout mlin = new LinearLayout(MainActivity.this);
                mlin.setLayoutParams(relalin);
                
                LinearLayout.LayoutParams rela = new LinearLayout.LayoutParams(
                        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
                rela.topMargin = 10;
                rela.bottomMargin = 10;
                TextView tv = new TextView(MainActivity.this);

                tv.setGravity(Gravity.CENTER);
                tv.setTextSize(26);
                tv.setText("日期選擇");
                mlin.addView(tv, rela);
                // 設(shè)置標(biāo)題
                builder.setCustomTitle(mlin);
                // 自定義布局
                View dialogv = inflater.inflate(R.layout.data, null);
                CalendarView cav = (CalendarView) dialogv.findViewById(R.id.calendarView1);
                cav.setOnDateChangeListener(new OnDateChangeListener() {

                    @Override
                    public void onSelectedDayChange(CalendarView view, int year,
                            int month, int dayOfMonth) {
                        // TODO Auto-generated method stub
                        //這里就是獲取得到年月日的信息 月要加一
                                                

                    }
                });
                                
                builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dia, int which) {
                        // TODO Auto-generated method stub
                                                    //這里點(diǎn)擊后進(jìn)行邏輯判斷
                        
                    }
                });

                builder.setNegativeButton("取消", null);
                builder.setView(dialogv);
                builder.create();
                dialog = builder.show();

            }
        });
       
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}


main.xml的布局文件就一個(gè)按鈕

<Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bt_1"
        android:text="點(diǎn)擊"/>    

data.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:orientation="vertical">
  
   <LinearLayout 
        android:layout_width="fill_parent"
       android:layout_height="300dp"
        android:orientation="horizontal"
        android:gravity="center">
   

        <CalendarView
            android:id="@+id/calendarView1"
            android:background="@color/lucency"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
      
  
</LinearLayout>

</LinearLayout>

基本上就這樣掂碱。反正我是花了很長(zhǎng)時(shí)間才看懂只是不想忘記就記下來(lái)了 第一次用簡(jiǎn)書還是有點(diǎn)小激動(dòng)!
http://pan.baidu.com/s/1skAxcOd

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末验庙,一起剝皮案震驚了整個(gè)濱河市顶吮,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌粪薛,老刑警劉巖悴了,帶你破解...
    沈念sama閱讀 218,755評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異违寿,居然都是意外死亡湃交,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門藤巢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)搞莺,“玉大人,你說(shuō)我怎么就攤上這事掂咒〔挪祝” “怎么了迈喉?”我有些...
    開封第一講書人閱讀 165,138評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)温圆。 經(jīng)常有香客問我挨摸,道長(zhǎng),這世上最難降的妖魔是什么岁歉? 我笑而不...
    開封第一講書人閱讀 58,791評(píng)論 1 295
  • 正文 為了忘掉前任得运,我火速辦了婚禮,結(jié)果婚禮上锅移,老公的妹妹穿的比我還像新娘熔掺。我一直安慰自己,他們只是感情好非剃,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評(píng)論 6 392
  • 文/花漫 我一把揭開白布置逻。 她就那樣靜靜地躺著,像睡著了一般努潘。 火紅的嫁衣襯著肌膚如雪诽偷。 梳的紋絲不亂的頭發(fā)上坤学,一...
    開封第一講書人閱讀 51,631評(píng)論 1 305
  • 那天疯坤,我揣著相機(jī)與錄音,去河邊找鬼深浮。 笑死压怠,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的飞苇。 我是一名探鬼主播菌瘫,決...
    沈念sama閱讀 40,362評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼布卡!你這毒婦竟也來(lái)了雨让?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,264評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤忿等,失蹤者是張志新(化名)和其女友劉穎栖忠,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體贸街,經(jīng)...
    沈念sama閱讀 45,724評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡庵寞,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了薛匪。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片捐川。...
    茶點(diǎn)故事閱讀 40,040評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖逸尖,靈堂內(nèi)的尸體忽然破棺而出古沥,到底是詐尸還是另有隱情瘸右,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評(píng)論 5 346
  • 正文 年R本政府宣布岩齿,位于F島的核電站尊浓,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏纯衍。R本人自食惡果不足惜栋齿,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望襟诸。 院中可真熱鬧瓦堵,春花似錦、人聲如沸歌亲。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)陷揪。三九已至惋鸥,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間悍缠,已是汗流浹背卦绣。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留飞蚓,地道東北人滤港。 一個(gè)月前我還...
    沈念sama閱讀 48,247評(píng)論 3 371
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像趴拧,于是被迫代替她去往敵國(guó)和親溅漾。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容