代码实现增加View对象

来源:互联网 发布:iptv与网络电视的区别 编辑:程序博客网 时间:2024/06/05 14:39
package com.example.touchevent;import android.os.Bundle;import android.annotation.SuppressLint;import android.app.Activity;import android.content.Context;import android.util.Log;import android.view.Menu;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.view.ViewGroup.LayoutParams;import android.widget.Button;import android.widget.LinearLayout;import android.widget.RelativeLayout;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);        LinearLayout main = (LinearLayout)findViewById(R.id.root);TextView tv = new TextView(this){@Overridepublic boolean onTouchEvent (MotionEvent event){Log.i("huangjs", ".................");switch (event.getAction()) {case MotionEvent.ACTION_DOWN:Toast.makeText(MainActivity.this, "ACTION_DOWN", Toast.LENGTH_LONG).show();break;case MotionEvent.ACTION_MOVE:Toast.makeText(MainActivity.this, "ACTION_MOVE", Toast.LENGTH_LONG).show();break;case MotionEvent.ACTION_UP:Toast.makeText(MainActivity.this, "ACTION_UP", Toast.LENGTH_LONG).show();break;default:break;}return false;}};        tv.setText("Hello, Android!");        main.addView(tv);   }}