2010年12月11日 星期六

Android-Button

Button(按鈕)

舉個範例程式:
這個程式在點下Button後字串會改變
blog.java


package blog.java;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class blog extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
 
     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button bt=(Button)findViewById(R.id.Button01);
        bt.setOnClickListener(this);
    }

 @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  TextView tv1=(TextView)findViewById(R.id.TextView01);
  tv1.setText("Hello EveryBody");
 }
}




Layout:main.xml
<?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:id="@+id/TextView01"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Button 
 android:text="變換" 
 android:id="@+id/Button01" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"></Button>
</LinearLayout>

沒有留言:

張貼留言