90 likes | 106 Views
動態版面. 建國科技大學 資管系 饒瑞佶 2017/7. 使用 OO 動態生成版面. DynamicLayout.java. 加入 implements OnGestureListener OnClickListener. DynamicLayout.java. private GestureDetector detector; // 宣告 GestureDetector 類物件 detector private ViewFlipper flipper; // 宣告 ViewFlipper 物件. @Override
E N D
動態版面 建國科技大學 資管系 饒瑞佶 2017/7
DynamicLayout.java • 加入implements • OnGestureListener • OnClickListener
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); }
// 設定版面 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; }
//處理按鈕事件 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; }
@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; }
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, "小夫"); }