package com.example.btnlistener2;

import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.view.Menu;

public class MainActivity extends Activity {
TextView tv;
Button woman1;
Button woman2;

@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("小美和小麗正站在你前面,你到底要選誰??!!");
woman1 = new Button(this);
woman1.setText("我選小美");
woman2 = new Button(this);
woman2.setText("我選小麗");

ll.addView(tv);
ll.addView(woman1);
ll.addView(woman2);

woman1.setOnTouchListener(new SampleTouchListener1());
woman2.setOnTouchListener(new SampleTouchListener2());
}

class SampleTouchListener1 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 SampleTouchListener2 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;
}
}

@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;
}

}

arrow
arrow

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