普通按钮和图片按钮
1.布局文件
<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:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/normal_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/normal_text"/>
<Button
android:id="@+id/small_button"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/small_text"/>
<ToggleButton
android:id="@+id/toggle_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/toggle_text"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/red"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/purple"
android:text="@string/button_text"/>
</LinearLayout>
2.代码文件
package com.example.button;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button normalButton,smalButton,toggButton;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
normalButton = (Button)findViewById(R.id.normal_button);
smalButton = (Button)findViewById(R.id.small_button);
toggButton = (Button)findViewById(R.id.toggle_button);
textView = (TextView) findViewById(R.id.text);
normalButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("Normall button is clicked!");
}
});
smalButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("small button is clicked!");
}
});
toggButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("toggle button is clicked!");
}
});
}
}