2013年12月26日 星期四

Android控制項的隱藏與顯示

Android控制項的隱藏與顯示


 


按照下面網址參考資料撰寫一個簡易的範例,歡迎有興趣的同好,一起來C/P一下。


 


資料來源:http://fecbob.pixnet.net/blog/post/35615328-android%E6%8E%A7%E5%88%B6%E9%A0%85%E7%9A%84%E9%9A%B1%E8%97%8F%E8%88%87%E9%A1%AF%E7%A4%BA


 









package com.example.button_hide_show;
import com.example.button_hide_show.MainActivity.ButListener;
/*
 Android控制項的隱藏與顯示
3個值
invisible:不顯示,但保留所占的空間
visible:正常顯示
gone:不顯示,且不保留所占的空間
 */

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import android.view.View;
///////////////////////////////////////

import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.TextView;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.view.ViewGroup.MarginLayoutParams;
public class MainActivity extends Activity {
Button[] bt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/////////////////////////////////////////
requestWindowFeature(Window.FEATURE_NO_TITLE); //全螢幕設定
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
/////////////////////////////////////////
AbsoluteLayout AbsLayout1=new AbsoluteLayout(this);
setContentView(AbsLayout1);
/////////////////////////////////////////
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
bt=new Button[4];

bt[0]=new Button(this);
bt[0].setText("0");
bt[0].setWidth(metrics.widthPixels/2);
bt[0].setHeight(metrics.heightPixels/2);
bt[0].setFocusable(true);
bt[0].setOnClickListener(new ButListener());


bt[0].setLayoutParams( new AbsoluteLayout.LayoutParams( metrics.widthPixels/2, metrics.heightPixels/2, 0, 0 ) );
AbsLayout1.addView(bt[0]);

bt[1]=new Button(this);
bt[1].setText("1");
bt[1].setWidth(metrics.widthPixels/2);
bt[1].setHeight(metrics.heightPixels/2);
bt[1].setOnClickListener(new ButListener());
bt[1].setLayoutParams( new AbsoluteLayout.LayoutParams( metrics.widthPixels/2, metrics.heightPixels/2, metrics.widthPixels/2, 0 ) );
AbsLayout1.addView(bt[1]);

bt[2]=new Button(this);
bt[2].setText("2");
bt[2].setWidth(metrics.widthPixels/2);
bt[2].setHeight(metrics.heightPixels/2);
bt[2].setOnClickListener(new ButListener());
bt[2].setLayoutParams( new AbsoluteLayout.LayoutParams( metrics.widthPixels/2, metrics.heightPixels/2, 0, metrics.heightPixels/2 ) );
AbsLayout1.addView(bt[2]);

bt[3]=new Button(this);
bt[3].setText("3");
bt[3].setWidth(metrics.widthPixels/2);
bt[3].setHeight(metrics.heightPixels/2);
bt[3].setOnClickListener(new ButListener());
bt[3].setLayoutParams( new AbsoluteLayout.LayoutParams( metrics.widthPixels/2, metrics.heightPixels/2, metrics.widthPixels/2, metrics.heightPixels/2 ) );
AbsLayout1.addView(bt[3]);
/////////////////////////////////////////
}

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

class ButListener implements OnClickListener
{
public void onClick(View v)
{
if(v==bt[0])
{
bt[0].setText("0 clicked...");
bt[0].setVisibility(View.INVISIBLE);//hide
bt[1].setVisibility(View.VISIBLE);//show
}
if(v==bt[1])
{
bt[1].setText("1 clicked...");
bt[1].setVisibility(View.INVISIBLE);//hide
bt[0].setVisibility(View.VISIBLE);//show
}
if(v==bt[2])
{
bt[2].setText("2 clicked...");
bt[2].setVisibility(View.INVISIBLE);//hide
bt[3].setVisibility(View.VISIBLE);//show
}
if(v==bt[3])
{
bt[3].setText("3 clicked...");
bt[3].setVisibility(View.INVISIBLE);//hide
bt[2].setVisibility(View.VISIBLE);//show
}
}

}
}


 


沒有留言:

張貼留言