使用eclipse创建安卓工程,假设工作名字使用button4,包使用com.mypack,在窗口中加入6个BUTTON.,使用默认命名,系统自动会命名为button1到button6,再加入一个editText,系统会自动命名为editText1.工程项目包名都可以随意。
资源的main.xml文件内容如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button2" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button3" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button4" android:onClick="Btn4OnClick" /> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button5" /> <Button android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button6" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="click button"> <requestFocus /> </EditText> </LinearLayout>
package com.mypack; import com.mypack.R; import android.app.Activity; import android.os.Bundle; import android.util.*; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import com.mypack.callOut; public class Button4Activity extends Activity implements OnClickListener { /** Called when the activity is first created. */ private Button m_button1, m_button2, m_button3, m_button4, m_button5, m_button6; public EditText ET; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); m_button1 = (Button) findViewById(R.id.button1); m_button2 = (Button) findViewById(R.id.button2); m_button3 = (Button) findViewById(R.id.button3); m_button4 = (Button) findViewById(R.id.button4); m_button5 = (Button) findViewById(R.id.button5); m_button6 = (Button) findViewById(R.id.button6); ET = (EditText) findViewById(R.id.editText1); /* * 方法1,其中的this相当于new OnClickListener()对象, 即class test 中的一个对象, * 而如果要用这种方式的话,public void onClick 方法必须写在该test类中, 且在开头使用implements * OnClickListener, 即this对象可以直接调用该方法 */ m_button1.setOnClickListener(this); //方法2,使用对象clickListener m_button2.setOnClickListener(clickListener); //方法3,使用匿名对象创建监听,同方法论,可以看作另一种写法 m_button3.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { String strTmp = "点击Button03"; ET.setText(strTmp); } }); //方法4,使用XML文件创建时绑定方法Btn4OnClick //方法5,自己设计个监听类,监听的方法引用OnClickListener接口中的方法,创建的是匿名对象 m_button5.setOnClickListener(new clickListener2()); //方法6, 外部类实现事件监听器接口,很少用 ,详看文件callout.java m_button6.setOnClickListener(new callOut(this)); } @Override public void onClick(View v) { Log.i("log", "click"); String strTmp = "点击Button01"; ET.setText(strTmp); } public OnClickListener clickListener = new OnClickListener() { public void onClick(View v) { Log.i("log", "click"); String strTmp = "点击Button02"; ET.setText(strTmp); } }; public class clickListener2 implements View.OnClickListener { public void onClick(View v) { Log.i("log", "click"); String strTmp = "点击Button05"; ET.setText(strTmp); } }; public void Btn4OnClick(View view) { String strTmp = "点击Button04"; ET.setText(strTmp); } }文件中最后一个按钮使用了类callOut,CALLOUT.java代码如下:
package com.mypack; import android.app.Activity; import android.view.View; import android.view.View.OnClickListener; import com.mypack.Button4Activity; public class callOut implements OnClickListener { private Button4Activity act; public callOut(Activity activity) { act = (Button4Activity)activity; } @Override public void onClick(View v) { String strTmp = "点击Button06"; act.ET.setText(strTmp); } }
http://www.eefocus.com/bbs/article_867_193559.html
http://blog.csdn.net/wyw1213/article/details/6282277