ANDROID_MARS学习笔记_S01_002View监听器初步
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ANDROID_MARS学习笔记_S01_002View监听器初步相关的知识,希望对你有一定的参考价值。
一、View、监听器介绍
二、在Activity中获取view和设置属性,设置button的监听器
1.activity_main.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 android:orientation="vertical" 10 tools:context="com.tony.androidfirst.MainActivity" > 11 12 <TextView 13 android:id="@+id/textView" 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:textSize="30sp" 17 android:background="#FF0000" 18 android:text="Hello android" /> 19 20 <Button 21 android:id="@+id/button" 22 android:layout_width="match_parent" 23 android:layout_height="wrap_content" 24 android:text="button"/> 25 26 27 </LinearLayout>
2.MainActivity.java
1 package com.tony.androidfirst; 2 3 import android.annotation.SuppressLint; 4 import android.graphics.Color; 5 import android.os.Bundle; 6 import android.support.v7.app.ActionBarActivity; 7 import android.view.Menu; 8 import android.view.MenuItem; 9 import android.view.View; 10 import android.view.View.OnClickListener; 11 import android.widget.Button; 12 import android.widget.TextView; 13 14 15 @SuppressLint("NewApi") 16 public class MainActivity extends ActionBarActivity { 17 18 private TextView textView; 19 private Button button; 20 int count = 0; 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.activity_main); 25 26 TextView textView = (TextView) findViewById(R.id.textView); 27 textView.setText("Hello View"); 28 textView.setBackgroundColor(Color.CYAN); 29 30 button = (Button) findViewById(R.id.button); 31 ButtonListener buttonListener = new ButtonListener(); 32 button.setOnClickListener(buttonListener); 33 34 } 35 36 class ButtonListener implements OnClickListener { 37 38 @Override 39 public void onClick(View v) { 40 count++; 41 System.out.println(count); 42 //textView.setText(count+""); 43 } 44 45 }
3.R.java
以上是关于ANDROID_MARS学习笔记_S01_002View监听器初步的主要内容,如果未能解决你的问题,请参考以下文章
ANDROID_MARS学习笔记_S05_002_给传感器注册listener
ANDROID_MARS学习笔记_S04_002_用AsyncTask实现异步操作
ANDROID_MARS学习笔记_S01_006ImageView
ANDROID_MARS学习笔记_S01_012_RatingBar