안드로이드 스튜디오
안드로이드 스튜디오 event 처리 3
;세미콜론;
2020. 4. 18. 12:11
반응형
커스텀 버튼
동그라미 쳐진 부분에 이미지 파일을 넣어 줍니다.
drawable 좌클릭후 리소스 파일을 새로 만들어 줍니다. (이 파일의 이름을 잘 기억 하세요)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/android_pressed"
android:state_pressed="true"/>
<item android:drawable="@drawable/android_focused"
android:state_focused="true"/>
<item android:drawable="@drawable/android_normal"/>
</selector>
상황에 따른 이미지 지정
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/button"
android:layout_margin="50dp"
android:text="" />
</LinearLayout>
package com.example.event4;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"click",Toast.LENGTH_SHORT).show();
}
});
}
}
스위치 (SWITCH) 이벤트
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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:id="@+id/switch_V"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Visibility" />
<Switch
android:id="@+id/switch_B"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bold" />
<Switch
android:id="@+id/switch_C"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Color" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:textSize="50dp"
android:text="HELLO WORLD" />
</LinearLayout>
package com.example.switchevent;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Switch switch_v,switch_b,switch_c;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switch_v = findViewById(R.id.switch_V);
switch_b = findViewById(R.id.switch_B);
switch_c = findViewById(R.id.switch_C);
textView = findViewById(R.id.textView);
switch_v.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
textView.setVisibility(View.VISIBLE);
else
textView.setVisibility(View.INVISIBLE);
}
});
switch_b.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
textView.setTypeface(null, Typeface.BOLD);
else
textView.setTypeface(null, Typeface.NORMAL);
}
});
switch_c.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
textView.setTextColor(Color.RED);
else
textView.setTextColor(Color.BLACK);
}
});
}
}
+) 원하는 내용을 입력하고 스위치로 변화시키는 코드
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
android:hint="내용을 입력 해주세요" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="submit"
android:textSize="8dp"/>
</LinearLayout>
<Switch
android:id="@+id/switch_V"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Visibility" />
<Switch
android:id="@+id/switch_B"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bold" />
<Switch
android:id="@+id/switch_C"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Color" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:textSize="50dp"
android:text="HELLO WORLD" />
</LinearLayout>
package com.example.switchevent;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Switch switch_v,switch_b,switch_c;
TextView textView;
EditText editText;
Button bnt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.edittext);
bnt = findViewById(R.id.button);
switch_v = findViewById(R.id.switch_V);
switch_b = findViewById(R.id.switch_B);
switch_c = findViewById(R.id.switch_C);
textView = findViewById(R.id.textView);
switch_v.setChecked(true);
bnt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText(editText.getText());
}
});
switch_v.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
textView.setVisibility(View.VISIBLE);
else
textView.setVisibility(View.INVISIBLE);
}
});
switch_b.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
textView.setTypeface(null, Typeface.BOLD);
else
textView.setTypeface(null, Typeface.NORMAL);
}
});
switch_c.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
textView.setTextColor(Color.RED);
else
textView.setTextColor(Color.BLACK);
}
});
}
}
반응형