2014年9月18日 星期四

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

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


 


此範例為程式碼(04\Sample5)的改寫,利用一個LinearLayout、一個TextView和兩個RadioButton實作GUI界面,並且實作RadioButton事件反應和屬性設定範例,程式碼如下所示:


 


 


package com.jashsample;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
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.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
/*
 * RadioButton反應事件和屬性設定
 */
public class MainActivity extends Activity {
TextView tv;
RadioButton rb1, rb2;
RadioGroup rg;
@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("歡迎光臨。");

rb1 = new RadioButton(this);
rb2 = new RadioButton(this);

rb1.setText("汽車");
rb2.setText("卡車");

rg = new RadioGroup(this);

rg.addView(rb1);
rg.addView(rb2);

rb1.setChecked(false);//預設不要選擇
rb2.setChecked(false);

ll.addView(tv);
ll.addView(rg);

rb1.setOnClickListener(new SampleClickListener());
rb2.setOnClickListener(new SampleClickListener());
}
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 onKeyDown(int keycode, KeyEvent e)//直接在Activity上實作KeyDown事件
{
String str="";
switch(keycode)
{
case KeyEvent.KEYCODE_DPAD_UP:
str = "上";
tv.setText("按" + str + "。");
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
str = "下";
tv.setText("按" + str + "。");
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
str = "左";
tv.setText("按" + str + "。");
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
str = "右";
tv.setText("按" + str + "。");
break;
case KeyEvent.KEYCODE_BACK:
this.finish();//關閉自己
break;
}


return true;
}
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 SampleClickListener implements OnClickListener//按鈕事件反��類別
{
public void onClick(View v)
{
rb1.setChecked(false);
rb2.setChecked(false);
RadioButton tmp = (RadioButton) v;
tv.setText("我要買" + tmp.getText() + "。");
tmp.setChecked(true);
}
}
}


 




沒有留言:

張貼留言