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
- 성능지표
- jinja2
- Django
- 환경변수 설정
- 사이트 도메인
- openweathermap
- javascript
- 실시간 시계
- DB 데이터 저장
- MySQL 세팅
- OpenCV
- flask
- #ifndef
- #if
- OpenCV + Flask
- Action
- 콘솔 가상환경 # 콘솔 #가상환경
- VS Code
- #endif
- #ifdef
- 튜토리얼
- DB 데이터
- heroku
- bootstrap4 패키지
- PyQt5
- bootstrap
- 명령어
- href
- #else
- #undef
Archives
- Today
- Total
PROGRAMMING
Button, TextView, Toast 본문
1-1. MainActivity.java (익명 클래스 사용, 클래스 선언 없이 내부 클래스 사용, 메서드의 인자에서 익명의 클래스 사용)
package com.example.tutorial1;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Application;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class MainActivity3 extends AppCompatActivity {
Button testButton;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
textView = (TextView) findViewById(R.id.textView);
testButton = (Button) findViewById(R.id.testButton);
testButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "테스트", Toast.LENGTH_SHORT).show();
if(testButton.getText().equals("Button"))
{
testButton.setText("clicked");
textView.setText("clicked");
}
else
{
testButton.setText("Button");
textView.setText("Hello!");
}
}
});
}
}
1-2. MainActivity에 Event Listener 구현
package com.example.tutorial1;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Application;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class MainActivity3 extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Button testButton = (Button) findViewById(R.id.testButton);
testButton.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.testButton:
Toast.makeText(getApplicationContext(), "버튼클릭", Toast.LENGTH_SHORT).show();
break;
}
}
}
2. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity3">
<Button
android:id="@+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/testButton" />
</androidx.constraintlayout.widget.ConstraintLayout>
'Android' 카테고리의 다른 글
Navigation ※ ~ing (0) | 2020.10.27 |
---|---|
EditText, TextView 의 text 연결 (0) | 2020.10.27 |
안드로이드 실습 - 리스트, 종료 with AlertDialog (0) | 2020.10.27 |
안드로이드 실습 - 연산 어플 (0) | 2020.10.27 |
JVM, JRE, JDK, Gradle, Apache Groovy, Compile process (0) | 2020.10.26 |
Comments