1、Java文件
package com.fish.helloworld; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private TextView m_TextView; private backButton_OnClickListener backButtonListener = new backButton_OnClickListener(); private clearButton_OnClickListener clearButtonListener = new clearButton_OnClickListener(); class backButton_OnClickListener implements OnClickListener{ public void onClick(View v){ m_TextView.setText("back"); } } class clearButton_OnClickListener implements OnClickListener{ public void onClick(View v){ m_TextView.setText("clear"); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.skeleton_activity); m_TextView = (TextView)findViewById(R.id.text1); final Button backButton = (Button)findViewById(R.id.back); final Button clearButton = (Button)findViewById(R.id.clear); backButton.setOnClickListener(backButtonListener); clearButton.setOnClickListener(clearButtonListener); } }
2、XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/text1" /> <Button android:id="@+id/back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/back" /> <Button android:id="@+id/clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/clear" /> </LinearLayout>