Download presentation
Presentation is loading. Please wait.
1
動態版面 建國科技大學 資管系 饒瑞佶 2017/7
2
使用OO動態生成版面
3
DynamicLayout.java 加入implements OnGestureListener OnClickListener
4
DynamicLayout.java private GestureDetector detector; //宣告GestureDetector類物件detector private ViewFlipper flipper; //宣告ViewFlipper物件 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); //取消原版面 flipper = new ViewFlipper(this); //建立ViewFlipper物件 // 在ViewFlipper物件中加入版面 flipper.addView(addObj(1)); //版面1 flipper.addView(addObj(2)); //版面2 flipper.addView(addObj(3)); //版面3 setContentView(flipper); //設定顯示畫面1 // 加入監聽物件 detector = new GestureDetector(this); }
5
// 設定版面 public View addObj(int i){ LinearLayout output = new LinearLayout(this); switch (i) { case 1: //版面1 Button btn = new Button(this); btn.setId(1); btn.setText("我是版面1"); btn.setOnClickListener(this); output.addView(btn); break; case 2: //版面2 //建立一個ImageView ImageView imgv= new ImageView(this); imgv.setBackgroundResource(R.drawable.ic_launcher); //設定圖示 //設定ImageView的格式 RelativeLayout.LayoutParams forimgv = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); //將ImageView加入LinearLayout output.addView(imgv,forimgv); case 3: //版面3 //建立一個TextView TextView tv = new TextView(this); tv.setText("我是版面3"); tv.setTextColor(Color.BLUE); output.addView(tv); } return output;
6
public void onClick(View v) { switch(v.getId()){ case 1:
//處理按鈕事件 public void onClick(View v) { switch(v.getId()){ case 1: Toast.makeText(MainActivity.this, "按鈕1", Toast.LENGTH_LONG).show(); break; case 2: Toast.makeText(MainActivity.this, "按鈕2", Toast.LENGTH_LONG).show(); default: } //處理滑動事件 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) { if (e1.getX() - e2.getX() > 120) { //右往左滑 this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_in)); This.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_out)); this.flipper.showNext(); return true; } else if (e1.getX() - e2.getX() < -120) { //左往右滑 this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.push_right_in)); this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.push_right_out)); this.flipper.showPrevious(); }
7
@Override public boolean onTouchEvent(MotionEvent event) { return this.detector.onTouchEvent(event); } public boolean onDown(MotionEvent e) { // TODO Auto-generated method stub return false; public void onLongPress(MotionEvent e) { public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) { public void onShowPress(MotionEvent e) { public boolean onSingleTapUp(MotionEvent e) {
8
長按
9
code @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LinearLayout windowLayout = (LinearLayout) findViewById(R.id.windowLayout); //註冊長按選單 this.registerForContextMenu(windowLayout); } public boolean onContextItemSelected(MenuItem item) { //當使用者點選項目時,所需的動作 Toast.makeText(this, "您選擇的是"+item.getTitle(), Toast.LENGTH_SHORT).show(); return super.onContextItemSelected(item); public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { //設定選單內容 super.onCreateContextMenu(menu, v, menuInfo); menu.add(0, 0, 0, "大雄"); menu.add(0, 1, 0, "小叮噹"); menu.add(0, 2, 0, "技安"); menu.add(0, 3, 0, "小夫");
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.