<?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"
android:background="#0f0">
<ImageView
android:src="@drawable/ic_launcher_background"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="150dp">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp"
android:textColor="#0f0"
android:text="Hello World!"/>
<Button
android:backgroundTint="#ff0"
android:text="More Information!"
android:textSize="30dp"
android:layout_marginLeft="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
기본 레이아웃 리니어로 변경, 배경, 텍스트 정도 했음
버튼 정렬을 위해서 강의에서는 layout_gravity="center_horizontal"을 사용, 마진이나 패딩을 주는걸로도 가능
3주차
버튼 만들어서 클릭시 팝업 텍스트, 기본 텍스트 변경하는 기능
package com.example.mobile;
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.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button btn1;
TextView title;
EditText editText1;
static int tmp = 0;
final InputMethodManager imm= (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
title = findViewById(R.id.title);
btn1 = findViewById(R.id.btnclick);
editText1 = findViewById(R.id.edit1);
// title.setText("안드로이드");
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//toast - 팝업 텍스트
if (tmp == 0){
title.setText("btn Clicked!");
title.setTextColor(Color.BLUE);
tmp = 1;
} else if (tmp == 1) {
title.setText("alreay Cliceked!");
title.setTextColor(Color.GREEN);
tmp = 0;
}
Toast.makeText(MainActivity.this, "안드로이드 스터디", Toast.LENGTH_LONG).show();
String str = editText1.getText().toString();
btn1.setText(str);
//버튼 클릭하면 키보드 창 숨기기
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),0);
}
});
}
}
<?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">
<TextView
android:id="@+id/title"
android:textColor ="#f00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="37dp"
android:text="위젯 이벤트 처리하기"/>
<EditText
android:id="@+id/edit1"
android:hint="name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnclick"
android:text="click"
android:textSize="30dp"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btn1"/>
</LinearLayout>
+계산기 만드는 코드 보고 정리해두기