生活應用: android studio BUTTON使用

2018年4月6日 星期五

android studio BUTTON使用

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="wrap_content"
    android:padding="10dip"
    >
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/TextView01"
    android:text="Steve Jobs Famous Quotes"
    android:layout_marginTop="10sp"
    android:textSize="20sp"
    />
    <Button
    android:layout_width="wrap_content"
android:layout_height="wrap_content"
    android:layout_marginTop="10sp"
    android:text=" Click me! "
    android:id="@+id/Button01"
    />
</LinearLayout>
Java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
publicclass TestExam001 extends Activity { 
    /** Called when the activity is first created. */
    private Button button01;
    private TextView textView01; 
    @Override
    publicvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);       
        button01 = (Button)findViewById(R.id.Button01);
        textView01 = (TextView)findViewById(R.id.TextView01);       
        button01.setOnClickListener(new Button.OnClickListener(){ 
            @Override
            publicvoid onClick(View v) {
                // TODO Auto-generated method stub
                textView01.setText("Stay Hungry, Stay Foolish. ");
            }         
        });     
    }
}

沒有留言:

張貼留言