에디트 텍스트와 버튼의 온클릭 이벤트를 이용해서 버튼을 클릭하면 에디트 텍스트의 텍스트가 버튼에 토스트 메세지로 나오는 코드
<EditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="텍스트 입력"
android:textAlignment="center"
android:inputType="text"/>
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="텍스트 보이기"/>
EditText editText = findViewById(R.id.edit1);
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String str = editText.getText().toString();
Toast.makeText(getBaseContext(),str,Toast.LENGTH_LONG).show();
}
});
좌표값 받아오는 코드
package com.example.AAP;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
class MyView extends View {
int key;
String str;
int x,y;
public MyView(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent event){
x = (int) event.getX(0);
y = (int) event.getY(0);
invalidate();
return super.onTouchEvent(event);
}
@Override
protected void onDraw(Canvas canvas){
Paint paint = new Paint();
paint.setTextSize(50);
canvas.drawCircle(x,y,30,paint);
canvas.drawText("(" + x + ", " + y + ") 2019026380 이찬호", x, y + 100, paint);
}
}
public class MainActivity extends AppCompatActivity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
MyView w = new MyView(this);
setContentView(w);
}
}