Activity到底是什么怎么用,到现在感觉心里还是没底的,说不太清楚,看文档的时候发现一大堆方法,这里我只写了简单布局的切换,至于以后Activity的用法方法,我会慢慢学习的。
1、什么是activity
Activity是一种互动的界面,一个界面可能有多个Activity组成。
2、演示activity的键盘及触摸事件
下面演示的是一个当键盘按下布局切换,键盘弹出布局切换的一个效果,已经触摸事件的掩饰Info信息
public class ActivityTestActivity extends Activity {
/** Called when the activity is first created. */
private final static String TAG="myTag";
boolean flag = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(flag){
this.setContentView(R.layout.second);
}else{
this.setContentView(R.layout.main);
}
Log.i(TAG, "onkeydown");
showInfo("keydown"+keyCode);
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
flag = !flag;
Log.i(TAG, "onkeyUp");
showInfo("keyup"+keyCode);
return super.onKeyUp(keyCode, event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.i(TAG, "onTouchEvent");
showInfo("keytouch"+event.getX()+" "+event.getY());
return super.onTouchEvent(event);
}
public void showInfo(String info){
Toast.makeText(this, info, Toast.LENGTH_SHORT).show();
}
}
/** Called when the activity is first created. */
private final static String TAG="myTag";
boolean flag = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(flag){
this.setContentView(R.layout.second);
}else{
this.setContentView(R.layout.main);
}
Log.i(TAG, "onkeydown");
showInfo("keydown"+keyCode);
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
flag = !flag;
Log.i(TAG, "onkeyUp");
showInfo("keyup"+keyCode);
return super.onKeyUp(keyCode, event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.i(TAG, "onTouchEvent");
showInfo("keytouch"+event.getX()+" "+event.getY());
return super.onTouchEvent(event);
}
public void showInfo(String info){
Toast.makeText(this, info, Toast.LENGTH_SHORT).show();
}
}
3、实现一个activity中多个布局页面的切换
a、利用按钮实现
通过给button添加事件监听器onClickListener,是button直接的方法互相调用,从而实现两个界面的切换。
package cn.csdn.hb;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Change extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button1();
}
public void Button1() {
setContentView(R.layout.second);
Button btn2 = (Button) findViewById(R.id.button2);
btn2.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Button2();
}
});
}
public void Button2() {
setContentView(R.layout.main);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Button1();
}
});
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Change extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button1();
}
public void Button1() {
setContentView(R.layout.second);
Button btn2 = (Button) findViewById(R.id.button2);
btn2.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Button2();
}
});
}
public void Button2() {
setContentView(R.layout.main);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Button1();
}
});
}
}
b、利用键盘或触摸实现
4、不同activity间的切换
如果想使两个Activity切换,那么肯定要使用我们具有桥梁作用的Intent
下面我分别写了两个xml文件,每个xml中分别放置了两个button,分别为button1和button2,在这里就不写了,我们将要实现的效果是当点击第一个Activity里面的button时切换到第2个Activity,当点击到第2个Activity里的button时,切换到第一个Activity。
如图:
第一个Activity:
public class ActivityTestActivity extends Activity {
/** Called when the activity is first created. */
private final static String TAG="myTag";
boolean flag = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bn = (Button)findViewById(R.id.button1);
bn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建启动的Activity对应的Intent
Intent intent =new Intent(ActivityTestActivity.this,SecondActivity.class);
//动intent对应的Activity
startActivity(intent);
}
});
}
第二个Activity:
package song.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SecondActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Button bn2 = (Button)findViewById(R.id.button2);
bn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(SecondActivity.this,ActivityTestActivity.class);
startActivity(intent);
}
});
}
}