一、简介
1.普通button
2.ImgeButton(带图片的button)
3.button(图片+文字)
4.ToggleButton (具有选择性的)
5.Switch
二、界面显示
三、布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="图标按钮:"/>
<ImageButton
android:id="@+id/button2"
android:layout_marginLeft="50dp"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/ro2"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮3"
android:drawableLeft="@drawable/get"/>
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="这里是显示内容"/>
<!--默认为未选中的状态。textOn,textOff,分别表示在选中和未选中状态所呈现的状态-->
<ToggleButton
android:layout_marginTop="20dp"
android:id="@+id/togButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="已选中"
android:textOff="未选中"/>
<Switch
android:id="@+id/btn_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="关"
android:textOn="开"/>
<TextView
android:layout_marginTop="20dp"
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text=""
android:gravity="center"/>
</LinearLayout>
四、事件代码
package com.example.asus.button_two_test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
//声明变量
private Button button1,button3;
private ImageButton button2;
private TextView text1,text2;
private ToggleButton togButton;
private Switch btn_switch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//当界面被创建时,就可以为变量赋值定义
button1=(Button)findViewById(R.id.button1);
button2=(ImageButton)findViewById(R.id.button2);
button3=(Button)findViewById(R.id.button3);
togButton=(ToggleButton)findViewById(R.id.togButton);
btn_switch=(Switch)findViewById(R.id.btn_switch);
//
text1=(TextView)findViewById(R.id.text1);
text2=(TextView)findViewById(R.id.text2);
//设置响应事件
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
text1.setText("按钮1被点击,一个简单的button");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
text1.setText("按钮2被点击,一个图片button");
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
text1.setText("按钮3被点击,一个混合型button");
}
});
togButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(((ToggleButton)view).isChecked()){
text2.setText("togButton被选中");
}else{
text2.setText("togButton未被选中");
}
}
});
btn_switch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(((Switch)view).isChecked()){
text2.setText("switch被选中");
}else{
text2.setText("switch未被选中");
}
}
});
}
}
有些时间段,熬过来就是大佬,熬不过来连菜鸟都不是