IT/Android

Widget - Switch

바바옄 2015. 6. 16. 16:06
반응형

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.ktds.myswitch;
 
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
 
 
public class MainActivity extends ActionBarActivity {
 
    private Switch aSwitch;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        aSwitch = (Switch) findViewById(R.id.swtMode);
        aSwitch.setOnCheckedChangeListener(new MySwitchChangeListener());
    }
 
    public class MySwitchChangeListener implements Switch.OnCheckedChangeListener{
 
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            String name = buttonView.getText().toString();
 
            if(isChecked){
                Toast.makeText(buttonView.getContext(),name+"선택됨",Toast.LENGTH_LONG).show();
            }
            else{
                Toast.makeText(buttonView.getContext(),name+"선택 해제됨",Toast.LENGTH_LONG).show();
            }
 
        }
    }
}
 
cs

 

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<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=".MainActivity">
 
 
    <Switch
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="New Switch"
        android:id="@+id/swtMode" />
</LinearLayout>
 
cs

 

             

 

반응형

'IT > Android' 카테고리의 다른 글

Widget - ProgressBar  (0) 2015.06.17
Widget - ImageView  (0) 2015.06.16
Widget - check box  (0) 2015.06.16
실습  (0) 2015.06.16
Widget - RadioButton  (0) 2015.06.16