android利用监听器来实现事件处理,有多种方法:
//以下下import 包部分省略,复制时请自行导入
- Activity本身作为事件监听器
activity_main.xml
<RelativeLayout 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:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.testevent.MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:text="Button" /> </RelativeLayout>
MainActivity.java
public class MainActivity extends Activity implements OnClickListener{ TextView textView; Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textView1); button = (Button) findViewById(R.id.button1); button.setOnClickListener(this); } @Override public void onClick(View v) { textView.setText("我被点击了"); } }
2.匿名内部类作为事件监听器
xml文件同上
MainActivity.java
public class MainActivity extends Activity { Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { TextView textView = (TextView) findViewById(R.id.textView1); textView.setText("我被点击了"); } }); } }
3.定义外部类作为事件监听器
xml文件同上
MainActivity.java
public class MainActivity extends Activity { TextView textView; Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.textView1); button = (Button) findViewById(R.id.button1); button.setOnLongClickListener(new TextChangeListener(this, textView)); } }
TextChangeListener.java
public class TextChangeListener implements OnLongClickListener { TextView textView; public TextChangeListener(Activity ac, TextView textView) { this.textView = textView; } @Override public boolean onLongClick(View v) { textView.setText("我被长时间点击了"); return true; } }
4.将事件监听器直接绑定到标签
activity_main.xml
<RelativeLayout 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:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.testevent.MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:onClick="buttonClickHandle" android:text="Button" /> </RelativeLayout>
MainActivity.java
public class MainActivity extends Activity { TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //此处必须是public public void buttonClickHandle(View v){ textView = (TextView)findViewById(R.id.textView1); textView.setText("我被点击了"); } }
时间: 2024-09-29 03:33:32