Interface: Using the Weight property, can be better to transplant to the tablet.
Layout code:
<?xml version= "1.0" encoding= "Utf-8"?>
<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"
tools:context= "Com.example.vibrator.MainActivity" >
<textview
Android:textsize= "22SP"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/title"/>
<linearlayout
android:orientation= "Horizontal"
android:layout_weight= "1"
Android:layout_width= "Match_parent"
android:layout_height= "0DP" >
<togglebutton
Android:layout_gravity= "Center_vertical"
android:layout_marginleft= "50DP"
Android:id= "@+id/button1"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
<textview
android:text= "@string/text2"
Android:textsize= "18DP"
Android:layout_gravity= "Center_vertical"
android:layout_marginleft= "30DP"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
</LinearLayout>
<linearlayout
android:orientation= "Horizontal"
android:layout_weight= "1"
Android:layout_width= "Match_parent"
android:layout_height= "0DP" >
<togglebutton
Android:layout_gravity= "Center_vertical"
android:layout_marginleft= "50DP"
Android:id= "@+id/button2"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
<textview
android:text= "@string/text1"
Android:textsize= "18DP"
Android:layout_gravity= "Center_vertical"
android:layout_marginleft= "30DP"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
</LinearLayout>
<linearlayout
android:orientation= "Horizontal"
android:layout_weight= "1"
Android:layout_width= "Match_parent"
android:layout_height= "0DP" >
<togglebutton
Android:layout_gravity= "Center_vertical"
android:layout_marginleft= "50DP"
Android:id= "@+id/button3"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
<textview
android:text= "@string/text3"
Android:textsize= "18DP"
Android:layout_gravity= "Center_vertical"
android:layout_marginleft= "30DP"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
</LinearLayout>
</LinearLayout>
Implement the vibration function code:
Be aware of adding permissions in Mainfest: <uses-permission android:name= "Android.permission.VIBRATE"/>
public class Mainactivity extends Appcompatactivity {
Create a vibrating service object
private vibrator Mvibrator;
Private ToggleButton Togglebutton1,togglebutton2,togglebutton3;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Init ();
Get mobile phone Shake service
mvibrator= (Vibrator) getapplication (). Getsystemservice (Service.vibrator_service);
Togglebutton1.setonclicklistener (New MyOnClickListener1 ());
Togglebutton2.setonclicklistener (New MyOnClickListener2 ());
Togglebutton3.setonclicklistener (New MyOnClickListener3 ());
}
private void init () {
togglebutton1= (ToggleButton) Findviewbyid (R.id.button1);
Togglebutton2= (ToggleButton) Findviewbyid (R.id.button2);
togglebutton3= (ToggleButton) Findviewbyid (R.id.button3);
}
Private class MyOnClickListener1 implements view.onclicklistener{
@Override
public void OnClick (View v) {
Decide whether to open
if (togglebutton1.ischecked ()) {
//Set the vibration period, the array represents the time: Wait + Execute, the unit is milliseconds, the following operation represents: Wait 100, execute 100, wait 100, execute
///After the number if 1 means not repeating, it executes once, the other delegates repeat, and 0 represents the beginning of the No. 0 position of the array
mvibrator.vibrate (new long[]{100,100,100,1000},-1);
Toast.maketext (Mainactivity.this,getstring (R.STRING.STR_OK), Toast.length_short). Show ();
}else{
//Remove vibrations
Mvibrator.cancel ();
Toast.maketext (Mainactivity.this,getstring (r.string.str_end), Toast.length_short). Show ();
}
}
}
Private class MyOnClickListener2 implements view.onclicklistener{
@Override
public void OnClick (View v) {
if (togglebutton2.ischecked ()) {
mvibrator.vibrate (new long[]{100,10,100,1000},0);
Toast.maketext (Mainactivity.this,getstring (R.STRING.STR_OK), Toast.length_short). Show ();
}else{
Mvibrator.cancel ();
Toast.maketext (Mainactivity.this,getstring (r.string.str_end), Toast.length_short). Show ();
}
}
}
Private class MyOnClickListener3 implements view.onclicklistener{
@Override
public void OnClick (View v) {
if (togglebutton3.ischecked ()) {
mvibrator.vibrate (new long[]{1000,50,1000,50,1000},0);
Toast.maketext (Mainactivity.this,getstring (R.STRING.STR_OK), Toast.length_short). Show ();
}else{
Mvibrator.cancel ();
Toast.maketext (Mainactivity.this,getstring (r.string.str_end), Toast.length_short). Show ();
}
}
}
}
Android to achieve mobile phone vibration function