1. URL 버튼 만들기
1-1. activity_main.xml
<?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">
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Button 1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="10"
android:text="Button 2" />
<!-- 임의로 넣은 코드 --!>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Go to Naver"
app:layout_contraintStart_toStartOf=”parent”
app:layout_contraintTop_toTopOf=”parent” />
</LinearLayout>
1-2. MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button urlButton = findViewById(R.id.button);
// 버튼 액션 추가
urlButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent urlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://naver.com"));
startActivity(urlIntent);
}
});
}
}
'안드로이드 > 연습 코드' 카테고리의 다른 글
6장. 서비스와 브로드캐스트 리시버 (0) | 2023.11.07 |
---|---|
5장. Fragment (0) | 2023.11.07 |
4장. Activity와 Intent (0) | 2023.11.07 |
3장. 위젯 (0) | 2023.11.07 |
2장. 뷰와 레이아웃 (0) | 2023.11.07 |