1.验证密码
mButton01.setOnClickListener(new mClick());
}
class mClick implements OnClickListener //定义一个类实现监听接口
{
public void onClick(View v)
{
String passwd;
passwd=edit.getText().toString();
if(passwd.equals("abc123"))
txt2.setText("欢迎进入快乐大本营!");
else
txt2.setText("非法用户,请立刻离开!");
}
}
2.使用imageview
ImageView imageview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageview = (ImageView) this.findViewById(R.id.mImageView);
imageview.setImageResource(R.drawable.img);
}
3.进度条控件
public class Ex03_10Activity extends Activity {
ProgressBar progressBar;
Button btn1, btn2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
progressBar = (ProgressBar)findViewById(R.id.progressBar1);
btn1 = (Button)findViewById(R.id.button1);
btn2 = (Button)findViewById(R.id.button2);
btn1.setOnClickListener(new mClick1());
btn2.setOnClickListener(new mClick2());
}
class mClick1 implements OnClickListener
{
public void onClick(View v) {
progressBar.incrementProgressBy(5);
}
}
class mClick2 implements OnClickListener
{
public void onClick(View v) {
progressBar.incrementProgressBy(-5);
}
}
}
4.checkbox
public class Ex03_11Activity extends Activity {
CheckBox ch1, ch2, ch3;
Button okBtn;
TextView txt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ch1 = (CheckBox)findViewById(R.id.checkBox1);
ch2 = (CheckBox)findViewById(R.id.checkBox2);
ch3 = (CheckBox)findViewById(R.id.checkBox3);
okBtn=(Button)findViewById(R.id.button1);
txt=(TextView)findViewById(R.id.textView2);
okBtn.setOnClickListener(new mClick());
}
class mClick implements OnClickListener
{
public void onClick(View v)
{
String str="";
if(ch1.isChecked()) str=str+"
"+ch1.getText();
if(ch2.isChecked()) str=str+"
"+ch2.getText();
if(ch3.isChecked()) str=str+"
"+ch3.getText();
txt.setText("您选择了:"+str);
}
}
}
5.toast
toast = Toast.makeText(MainActivity.this, //getApplicationContext(),
"自定义Toast的位置",
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
6.为多个按钮绑定事件
//为Button注册事件监听器
btn1.setOnClickListener(new mItemClick());
btn2.setOnClickListener(new mItemClick());
btn3.setOnClickListener(new mItemClick());
}
//定义列表选项监听器
class mItemClick implements OnClickListener
{
Toast toast;
LinearLayout toastView;
ImageView imageCodeProject;
@Override
public void onClick(View v)
{
if(v==btn1)
{
Toast.makeText(getApplicationContext(),
"默认Toast样式",
Toast.LENGTH_SHORT).show();
}
else if(v==btn2)
{
toast = Toast.makeText(MainActivity.this, //getApplicationContext(),
"自定义Toast的位置",
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
else if(v==btn3)
{
toast = Toast.makeText(MainActivity.this,
"带图标的Toast",
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 80);
toastView = (LinearLayout) toast.getView();
imageCodeProject = new ImageView(MainActivity.this);
imageCodeProject.setImageResource(R.drawable.icon);
toastView.addView(imageCodeProject, 0);
toast.show();
}
}
}
7.页面跳转
class MainActivity
btn.setOnClickListener(new mClick());
}
class mClick implements OnClickListener
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this, Second.class);
startActivity(intent);btn.setOnClickListener(new mClick());
}
class mClick implements OnClickListener
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this, Second.class);
startActivity(intent);
}
}
8.页面之间传递数据
mainactivity
btn1.setOnClickListener(new mClick());
}
class mClick implements OnClickListener
{
@Override
public void onClick(View v)
{
Intent intent = new Intent();
intent.setClass(MainActivity.this, secondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("text", txt.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
secondActivity
Bundle bundle2 = this.getIntent().getExtras();
String str = bundle2.getString("text");
txt2.setText(str);
btn2.setOnClickListener(new mClick());
}
class mClick implements OnClickListener
{
@Override
public void onClick(View v)
{
Intent intent2 = new Intent();
intent2.setClass(secondActivity.this, MainActivity.class);
startActivityForResult(intent2, 0);
}
}
9.使用菜单
public class MainActivity extends Activity
{
TextView txt;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView)findViewById(R.id.textView1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);//调用父类方法
//下面添加菜单项
menu.add(1, 1, 1, "菜单项1");
menu.add(1, 2, 2, "菜单项2");
menu.add(1, 3, 3, "菜单项3");
menu.add(1, 4, 4, "菜单项4");
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
//下面定义响应菜单项的事件
public boolean onOptionsItemSelected(MenuItem item)
{
String title = item.getTitle().toString();
switch(item.getItemId())
{
case 1:
txt.setText(title);
break;
case 2:
txt.setText(title);
break;
case 3:
txt.setText(title);
break;
case 4:
txt.setText(title);
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
}
10,上下文菜单
public class Ex04_04Activity extends Activity
{
TextView txt1, txt2, txt3;
private static final int item1 = Menu.FIRST;
private static final int item2 = Menu.FIRST+1;
private static final int item3 = Menu.FIRST+2;
String str[] = {"令狐冲", "杨 过", "萧 峰 " };
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt1=(TextView)findViewById(R.id.text1);
txt2=(TextView)findViewById(R.id.text2);
txt3=(TextView)findViewById(R.id.text3);
txt1.setText(str[0].toString());
txt2.setText(str[1].toString());
txt3.setText(str[2].toString());
registerForContextMenu(txt1);
registerForContextMenu(txt2);
registerForContextMenu(txt3);
}
//上下文菜单,本例会通过长按条目激活上下文菜单
@Override
public void onCreateContextMenu(ContextMenu menu, View view,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("人物简介");
//添加菜单项
menu.add(0, item1, 0, "武功");
menu.add(0, item2, 0, "战斗力");
menu.add(0, item3, 0, "经典语录");
}
//菜单单击响应
@Override
public boolean onContextItemSelected(MenuItem item){
//获取当前被选择的菜单项的信息
switch(item.getItemId())
{
case item1:
//在这里添加处理代码
break;
case item2:
//在这里添加处理代码
break;
case item3:
//在这里添加处理代码
break;
}
return true;
}
}
11.handler传递消息
public class MainActivity extends Activity
{
int i = 80, j = 10, step;
vView view;
Button btn;
Handler handler;
mThread thread;
private boolean STOP = true; //线程是否停止标志位
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.btn1);
btn.setOnClickListener(new mClick());
}
class mClick implements OnClickListener
{
@Override
public void onClick(View arg0)
{
thread = new mThread("abc");
handler = new mHandler();
STOP = false;//设置线程中循环的标志位
//开启新的线程
thread.start();
}
}
private class vView extends View
{
int x, y;
public vView(Context context, int x1, int y1)
{
super(context);
x=x1;
y=y1;
}
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawColor(Color.CYAN);//设置背景色为白色
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
canvas.drawCircle(x, y, 15, paint);//参数为坐标(x,y)、半径r、画笔
paint.setColor(Color.WHITE);
canvas.drawCircle(x-6, y-6, 3, paint);
}
}
//自定义的Handler类
private class mHandler extends Handler
{
public void handleMessage (Message msg)
{
switch(msg.what)
{
case 1:
{
step = step + 5;
//i = i + step;
j = j + step;
if(j>240) STOP = true; //设置停止线程中循环的标志位
break;
}
}
view = new vView(MainActivity.this, i, j);
setContentView(view);
}
}
//自定义的Thread类
private class mThread extends HandlerThread
{
public mThread(String name)
{
super(name);
// TODO Auto-generated constructor stub
}
@Override
//线程启动时执行这个函数
public void run()
{
//一直循环,直到标志位为“真”
while(!STOP)
{
try {
Thread.sleep(500);//延时0.5秒
} catch (InterruptedException e) {
e.printStackTrace();
}
Message msg = new Message();
//消息标志
msg.what = 1;
//发送这个消息
handler.sendMessage(msg);
}
}
}
}
12.layout转换
public class zhuanhuan extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/* 载入main.xml Layout */
setContentView(R.layout.main);
/* 以findViewById()取得Button对象,并添加onClickListener */
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
jumpToLayout2();
}
});
}
/* method jumpToLayout2:将layout由main.xml切换成mylayout.xml */
public void jumpToLayout2()
{
/* 将layout改成mylayout.xml */
setContentView(R.layout.mylayout);
/* 以findViewById()取得Button对象,并添加onClickListener */
Button b2 = (Button) findViewById(R.id.button2);
b2.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
jumpToLayout1();
}
});
}
/* method jumpToLayout1:将layout由mylayout.xml切换成main.xml */
public void jumpToLayout1()
{
/* 将layout改成main.xml */
setContentView(R.layout.main);
/* 以findViewById()取得Button对象,并添加onClickListener */
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
jumpToLayout2();
}
});
}
}
13.更新编辑框内容的变动
/*取得TextView、EditText*/
mTextView01 = (TextView)findViewById(R.id.myTextView);
mEditText01 = (EditText)findViewById(R.id.myEditText);
/*设置EditText用OnKeyListener事件来启动*/
mEditText01.setOnKeyListener(new EditText.OnKeyListener()
{
@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2)
{
// TODO Auto-generated method stub
/*设置TextView显示EditText所输入的内容*/
mTextView01.setText(mEditText01.getText());
return false;
}
});
14.ImageButton添加图片
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
//android:src="这里是给ImageButton加一个图片做这个imagebutton的图标"
在Activity里面监听这个ImageButton时间具体代码如下:
ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(this);
版权声明: