2015年6月8日 星期一

Android SlidingDrawer 滑動 視窗

上下左右的滑動視窗
1.res/layout/activity_main.xml


    

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
        

        
    

    

        

        
        
    


2.MainActivity.java
package tw.android;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class MainActivity extends Activity {

 private EditText etT, etB, etL, etR, etRotation;
 private Button btConfirm;
 private SlidingDrawer sd;
 private int left, top, right, bottom;

 /** Called when the activity is first created. */
 @SuppressLint("ShowToast")
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  etT = (EditText) findViewById(R.id.etT);
  etB = (EditText) findViewById(R.id.etB);
  etL = (EditText) findViewById(R.id.etL);
  etR = (EditText) findViewById(R.id.etR);
  etRotation = (EditText) findViewById(R.id.etRotation);
  btConfirm = (Button) findViewById(R.id.btConfirm);
  sd = (SlidingDrawer) findViewById(R.id.sd);

  /* 確認輸入 */
  btConfirm.setOnClickListener(new Button.OnClickListener() {

   public void onClick(View v) {
    /* 輸入SlidingDrawer距離上方位置 */

    left = Integer.parseInt(etL.getText().toString()); // 左
    top = Integer.parseInt(etT.getText().toString()); // 上
    right = Integer.parseInt(etR.getText().toString()); // 右
    bottom = Integer.parseInt(etB.getText().toString()); // 下

    if (left > 1000 || top > 1000 || right > 1000 || bottom > 1000) {
     Toast.makeText(getApplicationContext(), "不可大於1000",
       Toast.LENGTH_SHORT).show();

    } else {
     RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT,
       RelativeLayout.LayoutParams.WRAP_CONTENT);

     lp.setMargins(left, top, right, bottom);
     sd.setLayoutParams(lp);
     /* 輸入SlidingDrawer旋轉角度 */
     sd.setRotation(Integer.parseInt(etRotation.getText()
       .toString()));

     Toast.makeText(getApplicationContext(), "輸入完成",
       Toast.LENGTH_SHORT).show();
    }

   }
  });

  /* 打開監聽動作 */
  sd.setOnDrawerOpenListener(new OnDrawerOpenListener() {
   public void onDrawerOpened() {

    Toast.makeText(getApplicationContext(), "我被拉開了",
      Toast.LENGTH_SHORT).show();
    ;
   }
  });

  /* 關閉監聽動作 */
  sd.setOnDrawerCloseListener(new OnDrawerCloseListener() {

   public void onDrawerClosed() {
    Toast.makeText(getApplicationContext(), "我被關閉了",
      Toast.LENGTH_SHORT).show();
   }
  });

 }
}



參考來源:
http://examples.javacodegeeks.com/android/core/widget/slidingdrawer/android-slidingdrawer-example/
http://stackoverflow.com/questions/3695856/android-slidingdrawer-from-top

沒有留言 :

張貼留言