package com.example.usingdate;

import java.text.*;
import java.util.*;
import android.app.*;
import android.content.*;
import android.graphics.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.*;
import android.view.Menu;

public class MainActivity extends Activity {
TextView tv;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
setContentView(ll);

String[] str = new String[50];

tv = new TextView(this);
lv = new ListView(this);

tv.setText("歡迎光臨");

SampleAdapter ad = new SampleAdapter
(this, android.R.layout.simple_list_item_1, str);
lv.setAdapter(ad);

ll.addView(tv);
ll.addView(lv);

lv.setOnItemClickListener(new SampleItemClickListener());
}

class SampleItemClickListener implements OnItemClickListener
{
public void onItemClick(AdapterView<?>v, View iv, int pos, long id)
{
TextView tmp = (TextView) iv;
tv.setText("¬O" + tmp.getText() + "¹ï§a¡C");
}
}

class SampleAdapter extends ArrayAdapter<String>
{
public SampleAdapter(Context cn, int textViewResourceId,
String[] objects)
{
super(cn, textViewResourceId, objects);
}
public View getView(int pos, View convView, ViewGroup parent)
{

DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
Calendar cl = Calendar.getInstance();
cl.setTime(new Date());
cl.add(Calendar.DATE, pos);

TextView t = new TextView(getBaseContext());
t.setText(df.format(cl.getTime()));

if(cl.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
{
t.setBackgroundColor(Color.WHITE);
t.setTextColor(Color.RED);
}

convView = t;

return convView;
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

轉自Android程式設計實例入門

arrow
arrow
    創作者介紹
    創作者 痞客興 的頭像
    痞客興

    痞客興的部落格

    痞客興 發表在 痞客邦 留言(0) 人氣()