xml布局主要代码:
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:text="helloWorld" />
<!-- 为TextView添加点击事件需设置 clickable="true" -->
MainActivity主要代码:
public class MainActivity extends Activity implements OnClickListener {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
tv = (TextView)findViewById(R.id.tv);
tv.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
Toast.makeText(this, "我是helloworld", 1).show();
}
}
|
|