2014年9月25日 星期四

android程式設計實例入門 -Sample改寫分享 (2014/09/26)

android程式設計實例入門 -Sample改寫分享 (2014/09/26)


 


此範例為程式碼(04\Sample10)的改寫,利用一個LinearLayout、一個TextView和一個ListView實作GUI界面,並且實作自訂ArrayAdapter用來改變ListView顯示狀態的基本應用,程式碼如下所示:


 


package com.jashsample;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.method.KeyListener;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
/*
 * 自訂ArrayAdapter+ListView的基本應用,自訂ArrayAdapter,如此才可以控制顯示內容。
 */
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);


tv = new TextView(this);
tv.setText("歡迎光臨。");

lv = new ListView(this);
String[] str = {"汽車","卡車","敞篷車",
"計程車","跑車","金龜車",
"腳踏車","三輪車","摩��車",
"飛機","直升機","火箭"};
SampleAdapter ad = new SampleAdapter(this, android.R.layout.simple_list_item_1, str);//自訂ArrayAdapter,如此才可以控制顯示內容
lv.setAdapter(ad);

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

lv.setOnItemClickListener(new SampleItemClickListener());
}
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)//設定內容顯示狀態
{
ListView l = (ListView) parent;
String s = "---------" + l.getItemAtPosition(pos) ;

TextView t = new TextView(getBaseContext());
t.setText(pos + s);
t.setTextSize(20);

if(pos % 2 == 0)
{
t.setBackgroundColor(Color.WHITE);
t.setTextColor(Color.BLACK);
}
convView = t;

return convView;
}
}
/*
    public boolean onKey(View v, int keyCode, KeyEvent event)//當TextView有駐點時事件無效
    {
        tv.setText(et.getText());
        return false;
    }
    */
class SampleItemSelectedListener implements OnItemSelectedListener
{
public void onItemSelected(AdapterView<?>v, View iv, int pos, long id)
{
TextView tmp = (TextView) iv;
tv.setText("��" + tmp.getText() + "對吧。");
}
public void onNothingSelected(AdapterView<?> arg0){}
}
class SampleCheckedChangeListener implements OnCheckedChangeListener
{
public void onCheckedChanged(CompoundButton cb, boolean isChecked)
{
if(isChecked == true)
{
tv.setText("我要買" + cb.getText() + "。");
}
else if(isChecked == false)
{
tv.setText("我不買" + cb.getText() + "了。");
}
}
}
public boolean onTouchEvent(MotionEvent e)//直接在Activity上實作Touch事件
{
if(e.getAction() == MotionEvent.ACTION_DOWN)//設定在畫面任何地方Touch都改變tv文字
{
tv.setText("您好再見");
}
else if(e.getAction() == MotionEvent.ACTION_UP)
{
tv.setText("再見您好");
}
return true;
}
@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;
}
class SampleTouchListener implements OnTouchListener
{
public boolean onTouch(View v, MotionEvent e)
{
if(e.getAction() == MotionEvent.ACTION_DOWN)
{
tv.setText("您好");
}
else if(e.getAction() == MotionEvent.ACTION_UP)
{
tv.setText("再見");
}
return true;
}
}
class SampleItemClickListener implements OnItemClickListener
{
public void onItemClick(AdapterView<?>v, View iv, int pos, long id)
{
TextView tmp = (TextView) iv;
tv.setText("��" + tmp.getText() + "對吧。");
}
}
class SampleClickListener implements OnClickListener//按鈕事件反��類別
{
public void onClick(View v)
{
RadioButton tmp = (RadioButton) v;
tv.setText("我要買" + tmp.getText() + "。");
tmp.setChecked(true);
}
}
}












 




沒有留言:

張貼留言