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程式設計實例入門
留言列表