Toggle的控件用法:ToggleButton状态开关按钮
activity_toggle.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开启WLAN" android:textSize="20dp" /> <ToggleButton android:id="@+id/togglebutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="关闭" android:textOff="打开" /> </LinearLayout> </LinearLayout> ------------------------------------------------------------------------------------------------------- ToggleActivity.java package com.example.app6; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; import android.widget.ToggleButton; public class ToggleActivity extends Activity { //声明ToggleButton private ToggleButton togglebutton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_toggle); togglebutton = (ToggleButton) findViewById(R.id.togglebutton); togglebutton.setOnClickListener(new OnClickListener() { public void onClick(View v) { // 当按钮第一次被点击时候响应的事件 if (togglebutton.isChecked()) { Toast.makeText(ToggleActivity.this, "关闭", Toast.LENGTH_SHORT).show(); } // 当按钮再次被点击时候响应的事件 else { Toast.makeText(ToggleActivity.this, "打开", Toast.LENGTH_SHORT).show(); } } }); } } ----------------------------------------------------------------------------------------------------------------------------------------------------------
Switch控件的用法
activtiy.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Switch android:id="@+id/open" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="关闭" android:textOn="开启" /> </LinearLayout> ------------------------------------------------------------------------------------------------------ SwitchActivity.java package com.example.app6; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.widget.CompoundButton; import android.widget.Switch; import android.widget.Toast; public class SwitchActivity extends AppCompatActivity{ Switch open; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_switch); open=(Switch) findViewById(R.id.open); open.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b){ Toast.makeText(getApplicationContext(),"打开", Toast.LENGTH_SHORT).show(); }else { Toast.makeText(getApplicationContext(),"关闭",Toast.LENGTH_SHORT).show(); } } }); } } ---------------------------------------------------------------------------------------------------------SeekBar拖动进度条的使用及事件监听 <?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"> <SeekBar android:id="@+id/seekBar" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/description" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="拖动进度条" android:gravity="center_horizontal"/> </LinearLayout>-------------------------------------------------------------------------------------------------------
事件监听接口中有三个重要的方法:
SeekBarActivtiy.javapackage com.example.app6;import android.app.Activity;import android.os.Bundle;import android.widget.SeekBar;import android.widget.TextView;/** * Created by Administrator on 2017/6/6. */public class SeekBarActivity extends Activity{ private SeekBar seekBar; private TextView description; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_seekbar); seekBar=(SeekBar)findViewById(R.id.seekBar); description=(TextView)findViewById(R.id.description); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { description.setText("当前进度:"+progress+"%"); } @Override public void onStartTrackingTouch(SeekBar seekBar) { description.setText("开始拖动"); } @Override public void onStopTrackingTouch(SeekBar seekBar) { description.setText("拖动停止"); } }); }}-------------------------------------------------------------------------------------------------------RatingBar的控件用法<?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" > <RatingBar android:layout_width="wrap_content" android:layout_height="wrap_content" style="?android:attr/ratingBarStyleIndicator" android:id="@+id/ratingbar_Indicator" /> <RatingBar android:layout_width="wrap_content" android:layout_height="wrap_content" style="?android:attr/ratingBarStyle" android:id="@+id/ratingbar_default" /> </LinearLayout>-------------------------------------------------------------------------------------------------------RatingBarActivtiy.java package com.example.app6; import android.app.Activity; import android.os.Bundle; import android.widget.RatingBar; import android.widget.Toast; /** * Created by Administrator on 2017/6/6. */ public class RatingBarActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ratingbar); final RatingBar ratingBar_Indicator = (RatingBar)findViewById(R.id.ratingbar_Indicator); final RatingBar ratingBar_default = (RatingBar)findViewById(R.id.ratingbar_default); ratingBar_default.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener(){ public void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser) { ratingBar_Indicator.setRating(rating); Toast.makeText(RatingBarActivity.this, "rating:"+String.valueOf(rating), Toast.LENGTH_LONG).show(); }}); } }