|
3 | 3 | 仿IOS风格的开关,简洁易懂,只有一个类文件。
|
4 | 4 |
|
5 | 5 | 如何添加控件到项目
|
6 |
| - 下载此项目,拿到 |
| 6 | + |
| 7 | + 1:下载此项目,拿到SwitchButton这个类,复制到你的项目中。然后把自定义属性添加到你的attrs文件中 |
| 8 | + |
| 9 | +  |
| 10 | + |
| 11 | + 2:在你的布局文件中添加 |
| 12 | + |
| 13 | + |
| 14 | + <com.xie.brad.myswitchbutton.MyButton.SwitchButton |
| 15 | + android:id="@+id/switchbutton" |
| 16 | + android:layout_width="100dp" |
| 17 | + android:layout_height="wrap_content" |
| 18 | + android:layout_centerInParent="true" |
| 19 | + app:switch_state="true"/> |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + switch_state 设置按钮的开关状态。 |
| 24 | + |
| 25 | + 3:设置回调 |
| 26 | + |
| 27 | + |
| 28 | + package com.xie.brad.myswitchbutton; |
| 29 | + import android.os.Bundle; |
| 30 | + import android.support.v7.app.AppCompatActivity; |
| 31 | + import android.widget.Toast; |
| 32 | + import com.xie.brad.myswitchbutton.MyButton.SwitchButton; |
| 33 | + public class MainActivity extends AppCompatActivity implements SwitchButton.OnSwitchListener { |
| 34 | + |
| 35 | + @Override |
| 36 | + protected void onCreate(Bundle savedInstanceState) { |
| 37 | + super.onCreate(savedInstanceState); |
| 38 | + setContentView(R.layout.activity_main); |
| 39 | + SwitchButton switchButton = (SwitchButton) findViewById(R.id.switchbutton); |
| 40 | + switchButton.setOnSwitchListener(this);//设置button的点击回调 |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void openbutton() { |
| 45 | + Toast.makeText(this,"开",Toast.LENGTH_SHORT).show(); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void closebutton() { |
| 50 | + Toast.makeText(this,"关",Toast.LENGTH_SHORT).show(); |
| 51 | + } |
| 52 | + } |
0 commit comments