Strings.xml
<?xml version="1.0"
encoding="utf-8"?>
<resources>
<string name="app_name">ToggleButton2</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>
activity_main.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="67dp"
android:layout_marginTop="28dp"
android:text="ToggleButton" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/toggleButton1"
android:layout_below="@+id/toggleButton1"
android:layout_marginTop="66dp"
android:text="Click" />
</RelativeLayout>
MainActivity.java
import android.os.Bundle;
import
android.app.Activity;
import android.view.Menu;
import android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.Toast;
import
android.widget.ToggleButton;
public class MainActivity extends Activity {
ToggleButton
toggleButton;
Button
click;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton=(ToggleButton)findViewById(R.id.toggleButton1);
click=(Button)findViewById(R.id.button1);
click.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean status = toggleButton.isChecked();
if(status){
Toast.makeText(getApplicationContext(),
"Button
is on", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(),
"Button
is off", Toast.LENGTH_LONG).show();
}
}
});
}
}
No comments:
Post a Comment