๊ด€๋ฆฌ ๋ฉ”๋‰ด

Welcome! Everything is fine.

[Android] ๋ทฐ ๋ฐ”์ธ๋”ฉ ํ•˜๊ธฐ ๋ณธ๋ฌธ

Android

[Android] ๋ทฐ ๋ฐ”์ธ๋”ฉ ํ•˜๊ธฐ

๊ฐœ๋ฐœ๊ณฐ๋ฐœ 2022. 5. 14.
728x90

๐Ÿ“Œ ๋ทฐ ๋ฐ”์ธ๋”ฉํ•˜๊ธฐ

์ž๋ฐ”์—์„œ ๋ทฐ๋ฐ”์ธ๋”ฉ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž! Gradle Scripts > build.gradle(Module: ~)์—์„œ ์ ์ ˆํ•œ ์œ„์น˜์—(๋‚˜๋Š” buildTypes ์œ„์— ๋„ฃ์—ˆ๋‹ค.) ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ๋„ฃ๊ณ  Sync Now๋ฅผ ๋ˆŒ๋ €๋‹ค.

  buildFeatures {
      viewBinding = true
  }

๊ทธ๋ฆฌ๊ณ  MainActivity.java๋กœ ๋Œ์•„์™€์„œ private ActivityMainBinding binding; ์ด๋ผ๊ณ  ์„ ์–ธํ•ด์ค€ ํ›„ onCreate( )ํ•จ์ˆ˜ ์•ˆ์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ ์–ด์ค€๋‹ค.

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
}

๊ทธ๋Ÿฌ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด findViewById๋ฅผ ๋Œ€์ฒดํ•˜์—ฌ ๋” ๊ฐ„ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

  binding.button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
          println("๋ฒ„ํŠผ ๋ˆŒ๋ฆผ.");
      }
  });

์ฐธ๊ณ ๋กœ ์ฝ”ํ‹€๋ฆฐ์—์„œ๋Š” gradle์— ์ ๋Š” ๊ฒƒ์€ ๋˜‘๊ฐ™๊ณ , MainActivity.kt์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ทฐ๋ฐ”์ธ๋”ฉ ํ•œ๋‹ค.

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
 }