1.布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:orientation="vertical" android:paddingTop="@dimen/activity_vertical_margin" tools:context="helloworld.com.inspur.app4.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="hah" android:id="@+id/tv_1" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="hahahh" android:id="@+id/tv_2" /> </LinearLayout>
(2)逻辑代码的实现
package helloworld.com.inspur.app4; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private TextView tv1; private TextView tv2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv1=(TextView)findViewById(R.id.tv_1); tv2=(TextView)findViewById(R.id.tv_2); tv2.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_UP: Toast.makeText(MainActivity.this,"按下",Toast.LENGTH_SHORT).show(); break; case MotionEvent.ACTION_MOVE: Toast.makeText(MainActivity.this,"移动",Toast.LENGTH_SHORT).show(); break; case MotionEvent.ACTION_DOWN: Toast.makeText(MainActivity.this,"松开",Toast.LENGTH_SHORT).show(); break; } return true; } }); } }