1 / 9

動態版面

動態版面. 建國科技大學 資管系 饒瑞佶 2017/7. 使用 OO 動態生成版面. DynamicLayout.java. 加入 implements OnGestureListener OnClickListener. DynamicLayout.java. private GestureDetector detector; // 宣告 GestureDetector 類物件 detector private ViewFlipper flipper; // 宣告 ViewFlipper 物件. @Override

scottbeard
Download Presentation

動態版面

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 動態版面 建國科技大學 資管系 饒瑞佶 2017/7

  2. 使用OO動態生成版面

  3. DynamicLayout.java • 加入implements • OnGestureListener • OnClickListener

  4. DynamicLayout.java private GestureDetector detector; //宣告GestureDetector類物件detector private ViewFlipper flipper; //宣告ViewFlipper物件 @Override protectedvoid 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); break; case 3: //版面3 //建立一個TextView TextView tv = new TextView(this); tv.setText("我是版面3"); tv.setTextColor(Color.BLUE); output.addView(tv); break; } return output; }

  6. //處理按鈕事件 publicvoid 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(); break; default: break; } } //處理滑動事件 publicboolean 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(); returntrue; } elseif (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(); returntrue; } returntrue; }

  7. @Override publicboolean onTouchEvent(MotionEvent event) { returnthis.detector.onTouchEvent(event); } publicboolean onDown(MotionEvent e) { // TODO Auto-generated method stub returnfalse; } publicvoid onLongPress(MotionEvent e) { // TODO Auto-generated method stub } publicboolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) { // TODO Auto-generated method stub returnfalse; } publicvoid onShowPress(MotionEvent e) { // TODO Auto-generated method stub } publicboolean onSingleTapUp(MotionEvent e) { // TODO Auto-generated method stub returnfalse; }

  8. 長按

  9. code @Override protectedvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LinearLayout windowLayout = (LinearLayout) findViewById(R.id.windowLayout); //註冊長按選單 this.registerForContextMenu(windowLayout); } @Override publicboolean onContextItemSelected(MenuItem item) { //當使用者點選項目時,所需的動作 Toast.makeText(this, "您選擇的是"+item.getTitle(), Toast.LENGTH_SHORT).show(); returnsuper.onContextItemSelected(item); } @Override publicvoid 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, "小夫"); }

More Related