Activity到底是什麼怎麼用,到現在感覺心裡還是沒底的,說不太清楚,看文檔的時候發現一大堆方法,這裡我只寫了簡單布局的切換,至於以後Activity的用法方法,我會慢慢學習的。
1、什麼是activity
Activity是一種互動的界面,一個界面可能有多個Activity組成。
2、演示activity的鍵盤及觸摸事件
下面演示的是一個當鍵盤按下布局切換,鍵盤彈出布局切換的一個效果,已經觸摸事件的掩飾Info信息
view plain
- 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();
- }
-
- }
3、實現一個activity中多個布局頁面的切換
a、利用按鈕實現
通過給button添加事件監聽器onClickListener,是button直接的方法互相調用,從而實現兩個界面的切換。
view plain
- 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);
-
-
- Button btn1 = (Button) findViewById(R.id.button1);
- btn1.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- 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:
view plain
- 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:
view plain
- 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);
- }
- });
- }
-
-
- }