1. 程式人生 > >Android 一個按鈕兩次單擊觸發兩個不同事件並實現兩個View在同一介面切換

Android 一個按鈕兩次單擊觸發兩個不同事件並實現兩個View在同一介面切換

MenuText.java]

package com.lskl.com;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.LinearLayout;

public class MenuText extends Activity {

public static int flag = 0;

private LinearLayout.LayoutParams lp

;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.double_frame);

// Button but = getBtnBack();

lp = new LinearLayout.LayoutParams(150,

LinearLayout.LayoutParams.FILL_PARENT);

lp.setMargins(-150, 0, 0, 0);

((LinearLayout) findViewById(R.id.leftImage

)).setLayoutParams(lp);

initButton();

}

public void initButton() {

Button button01 = (Button) findViewById(R.id.btnBack01);

// Button but = getBtnBack();

button01.setText("測試");

button01.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (flag == 0) {

// 第一次單擊觸發的事件

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(

250, LinearLayout.LayoutParams.FILL_PARENT);

lp.setMargins(0, 0, 0, 0);

((LinearLayout) findViewById(R.id.leftImage))

.setLayoutParams(lp);

flag = 1;

} else {

// 第二次單擊button.text改變觸發的事件

       ((LinearLayout) findViewById(R.id.leftImage))

.setLayoutParams(lp);

flag = 0;

}

}

});

}

}

 [double_frame.xml]

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent" android:layout_height="fill_parent"

android:orientation="horizontal">

<LinearLayout android:id="@+id/leftImage"

android:layout_marginLeft="-220dp" android:layout_width="270dp"

android:background="@drawable/btn_dial_normal" android:layout_height="fill_parent">

<include android:id="@+id/include01" layout="@layout/base_layout_l"

android:layout_width="wrap_content" android:layout_height="fill_parent"></include>

</LinearLayout>

<LinearLayout android:id="@+id/rightImage"

android:layout_width="fill_parent" android:background="@drawable/btn_dial"

android:layout_height="fill_parent">

<include android:id="@+id/include02" layout="@layout/base_layout_r"

android:layout_width="wrap_content" android:layout_height="fill_parent"></include>

</LinearLayout>

</LinearLayout>

[base_layout_l.xml]

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"

   android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout android:id="@+id/linearTop"

       android:layout_height="wrap_content" android:layout_width="fill_parent"

       android:gravity="center">

<ImageView android:layout_height="wrap_content" android:id="@+id/imageView1"

           android:layout_width="wrap_content" android:src="@drawable/icon"></ImageView>

<TextView android:id="@+id/textView1" android:layout_width="wrap_content"

           android:layout_height="wrap_content" android:height="40dip"

           android:gravity="center" android:text="Dynamic Title"></TextView>

</LinearLayout>

<LinearLayout android:id="@+id/linearMain"

       android:layout_height="wrap_content" android:layout_width="fill_parent"

       android:layout_weight="1.0">

</LinearLayout>

<RelativeLayout android:layout_height="wrap_content"

       android:layout_width="fill_parent" android:id="@+id/relativeBottomTotal">

   <Button android:id="@+id/btnBack" android:layout_width="wrap_content"

           android:layout_height="wrap_content" android:text=" "></Button>

<LinearLayout android:layout_toRightOf="@+id/btnBack"

           android:layout_height="wrap_content" android:layout_alignTop="@+id/btnBack"

           android:layout_alignBottom="@+id/btnBack" android:id="@+id/layoutBottom"

           android:layout_width="fill_parent" android:gravity="right"></LinearLayout>

</RelativeLayout>

</LinearLayout>

[base_layout_r.xml]

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"

   android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout android:id="@+id/linearTop"

       android:layout_height="wrap_content" android:layout_width="fill_parent"

       android:gravity="center">

<ImageView android:layout_height="wrap_content" android:id="@+id/imageView1"

           android:layout_width="wrap_content" android:src="@drawable/icon"></ImageView>

<TextView android:id="@+id/textView1" android:layout_width="wrap_content"

           android:layout_height="wrap_content" android:height="40dip"

           android:gravity="center" android:text="Dynamic Title"></TextView>

</LinearLayout>

<LinearLayout android:id="@+id/linearMain"

       android:layout_height="wrap_content" android:layout_width="fill_parent"

       android:layout_weight="1.0">

</LinearLayout>

<RelativeLayout android:layout_height="wrap_content"

       android:layout_width="fill_parent" android:id="@+id/relativeBottomTotal">

<Button android:id="@+id/btnBack01" android:layout_width="wrap_content"

           android:layout_height="wrap_content" android:text=" "></Button>

<LinearLayout android:layout_toRightOf="@+id/btnBack"

           android:layout_height="wrap_content" android:layout_alignTop="@+id/btnBack"

           android:layout_alignBottom="@+id/btnBack" android:id="@+id/layoutBottom"

           android:layout_width="fill_parent" android:gravity="right"></LinearLayout>

</RelativeLayout>

</LinearLayout>

Android <wbr>一個按鈕兩次單擊觸發兩個不同事件並實現兩個View在同一介面切換

Android <wbr>一個按鈕兩次單擊觸發兩個不同事件並實現兩個View在同一介面切換