Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 안드로이드
- snowpack
- 오버라이드
- FIle
- degit
- 코틀린
- 미니게임
- 숫자
- 함수
- sub query
- React
- 게시판
- 스타일보험
- parcel
- 쿠키
- Android
- git
- Spring
- 스프링
- 상속
- java#왕초보
- webpack
- kotlin
- 버전일치
- 왕초보
- 답글
- java
- SQL
- Spinner
- 시큐어코딩
Archives
- Today
- Total
YSHUSH
Context menu 본문
1. app - src - res 에 menu경로 생성
2. menu폴더 우클릭 → menu 리소스 파일 → context_menu_main 생성
context_menu_main
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/text_color"
android:title="글자색 변경"/>
<item
android:id="@+id/text_back_color"
android:title="배경색 변경"/>
<item
android:id="@+id/text_bacic"
android:title="초기화"/>
</menu>
3. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ContextMenu 보기"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
4. build.gradle 추가
buildFeatures{
viewBinding true
}
5. MainActivity.kt
class MainActivity : AppCompatActivity() {
val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// setContentView(R.layout.activity_main)
setContentView(binding.root)
registerForContextMenu(binding.textView)
}
override fun onCreateContextMenu(menu: ContextMenu?, v: View?, menuInfo: ContextMenu.ContextMenuInfo?) {
menuInflater.inflate(R.menu.context_menu_main, menu)
}
override fun onContextItemSelected(item: MenuItem): Boolean {
val textView = findViewById<TextView>(R.id.textView)
when(item?.itemId){
R.id.text_color -> {
textView.text = "글자색 변경"
textView.setTextColor(Color.YELLOW)
}
R.id.text_back_color -> {
textView.text = "배경색 변경"
textView.setBackgroundColor(Color.BLUE)
}
R.id.text_bacic -> {
textView.text = "초기화"
textView.setBackgroundColor(Color.WHITE)
textView.setTextColor(Color.GRAY)
}
}
return super.onContextItemSelected(item)
}
}
'Coding > Android(kotlin)' 카테고리의 다른 글
Spinner2 (0) | 2022.02.08 |
---|---|
Spinner (0) | 2022.02.08 |
체크박스 (0) | 2022.02.08 |
라디오 버튼 (0) | 2022.02.08 |
버튼(Button) 클릭시 alert dialog띄우기 (0) | 2022.02.08 |