2017年4月11日 星期二

onclick()在xml的加入教學


這個程式是onclick的筆記
android:onClick="onClick"

public void onClick(View view){
     Toast.makeText(AndroidOnClickActivity.this,
       "Button Clicked", Toast.LENGTH_LONG).show();
    }


xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<Button 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Click Me"
    android:onClick="onClick"
    />
</LinearLayout>




?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.AndroidOnClick;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
 
public class AndroidOnClickActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
     
    public void onClick(View view){
     Toast.makeText(AndroidOnClickActivity.this,
       "Button Clicked", Toast.LENGTH_LONG).show();
    }
}

沒有留言: