黑馬66期android學習筆記16_按鈕點選事件
阿新 • • 發佈:2019-02-20
一、專案結構
二、activity_main.xml程式碼
<LinearLayout 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:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按鈕1" android:onClick="click" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按鈕2" android:onClick="click" /> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按鈕3" android:onClick="click" /> <Button android:id="@+id/button4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按鈕4" android:onClick="click" /> </LinearLayout>
三、MainActivity.java程式碼
package com.example.button.click; import android.support.v7.app.ActionBarActivity; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; public class MainActivity extends Activity { private static final String TAG="MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View v) { switch (v.getId()) { case R.id.button1: Log.i(TAG,"點選按鈕1"); break; case R.id.button2: Log.i(TAG,"點選按鈕2"); break; case R.id.button3: Log.i(TAG,"點選按鈕3"); break; case R.id.button4: Log.i(TAG,"點選按鈕4"); break; default: break; } } }
下載地址