[转]android-学习笔记之按钮事件
本文轉載自: http://zhangkun716717-126-com.iteye.com/blog/761080
?
前備知識: 1.需要了解怎么得到界面元素。
那么如何得到界面元素呢?在界面配置文件:例如 main.xml 中,比方一個id為idButtonTest1的Button定義如下:
?
<Button android:id="@+id/idButtonTest1"android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/button_test1"/> <Button android:id="@+id/idButtonTest2"android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/button_test2"/>?
?
在string常量配置文件string.xml中,配置如下常量
?
<string name="button_test1">測試按鈕1</string><string name="button_test2">測試按鈕2</string>??
?
?
那么得到該Button的做法就是findViewById(R.id.idButtonTest1); 比如:
?
Button buttonTest1 = (Button)findViewById(R.id.idButtonTest1);?
buttonTest1就是那個id為idButtonTest1的Button了。
好了,下邊開始今天的主題。今天講的主要是OnClickListener,該類位置:import android.view.View.OnClickListener; 那么如何給剛才那個Button添加click事件呢?代碼如下:
?
buttonTest1.setOnClickListener(newOnClickListener);?
這里的newOnClickListener是一個OnClickListener對象:
private OnClickListener newOnClickListener = new OnClickListener(){@Overridepublic void onClick(View v) {Toast.makeText(demo2.this, "Toast:Button_Test1", Toast.LENGTH_SHORT).show();} };???
?
?
這是比較羅嗦,或者說是比較婆媽的做法,實際使用中我們可以簡寫為:
?
findViewById(R.id.idButtonTest1).setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {Toast.makeText(demo2.this, "Toast:Button_Test1", Toast.LENGTH_SHORT).show();} });??
?
不過不推薦直接用findViewById(R.id.idButtonTest1),不規范呀么不規范~ 還有一種則是把全部的click事件給一個clickHandler來處理:
?
public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Button buttonTest1 = (Button)findViewById(R.id.idButtonTest1);Button buttonTest2 = (Button)findViewById(R.id.idButtonTest2);buttonTest1.setOnClickListener(clickHandler);buttonTest2.setOnClickListener(clickHandler); }private OnClickListener clickHandler = new OnClickListener(){@Overridepublic void onClick(View v) {int id = v.getId();switch(id){case R.id.idButtonTest1:Toast.makeText(demo2.this, "Toast:Button_Test1", Toast.LENGTH_SHORT).show();break;case R.id.idButtonTest2:Toast.makeText(demo2.this, "Toast:Button_Test2", Toast.LENGTH_SHORT).show();break;default:Toast.makeText(demo2.this, "Toast:none", Toast.LENGTH_SHORT).show();}} };?
?
Android SDK 2.0之后的版本提供了更簡潔的方法,在Activity里創建一個public方法(記得設置View參數),然后在 Layout方法里直接設置。 邏輯和設計相比上邊的簡寫和findViewById方法都更簡單明了,更具可讀性,也方便以后維護。
?
方式一:在配置中定義不同onClick事件方法
?
Activity中代碼:
public void myClickButton1(View v){Toast.makeText(this, "Toast:Button_Test1", Toast.LENGTH_SHORT).show();}public void myClickButton2(View v){Toast.makeText(this, "Toast:Button_Test2", Toast.LENGTH_SHORT).show();}?
在main.xml中配置內容如下:
?
<Button android:id="@+id/idButtonTest1"android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/button_test1"android:onClick="myClickButton1"/> <Button android:id="@+id/idButtonTest2"android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/button_test2"android:onClick="myClickButton2"/>?
注意:main.xml中的配置android:onClick="myClickButton1"和android:onClick="myClickButton2",如果熟悉JavaScript的朋友就會發現,這和JavaScript的onclick事件定義方法一模一樣啊! :-)
?
?
方式二:在配置中定義相同的onClick事件方法,然后在onClick方法中根據不同的按鈕執行不同的操作。
?
?
Activity中代碼:
?
public void myClickHandler(View v){int id = v.getId();switch(id){case R.id.idButtonTest1:Toast.makeText(demo2.this, "Toast1:Button_Test1", Toast.LENGTH_SHORT).show();break;case R.id.idButtonTest2:Toast.makeText(demo2.this, "Toast2:Button_Test2", Toast.LENGTH_SHORT).show();break;default:Toast.makeText(demo2.this, "Toast:none", Toast.LENGTH_SHORT).show();} }?
?
main.xml配置內容如下:
<Button android:id="@+id/idButtonTest1"android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/button_test1"android:onClick="myClickHandler"/> <Button android:id="@+id/idButtonTest2"android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/button_test2"android:onClick="myClickHandler"/>?
?
?
轉載于:https://www.cnblogs.com/freeliver54/p/3631583.html
總結
以上是生活随笔為你收集整理的[转]android-学习笔记之按钮事件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#正则表达式提取HTML中IMG标签的
- 下一篇: sencha touch 组件选择器ge