Skip to content

Commit

Permalink
Lesson 5
Browse files Browse the repository at this point in the history
  • Loading branch information
atuyan39 committed Jun 12, 2021
1 parent 251756d commit fe5bf66
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 2 deletions.
5 changes: 3 additions & 2 deletions app/src/main/AndroidManifest.xml
Expand Up @@ -13,14 +13,15 @@
<activity android:name=".Lesson1Activity" />
<activity android:name=".Lesson2Activity" />
<activity android:name=".Lesson3Activity" />
<activity android:name=".Lesson4Activity">
<activity android:name=".Lesson4Activity" />
<activity android:name=".Lesson4SubActivity" />
<activity android:name=".Lesson5Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Lesson4SubActivity" />
</application>

</manifest>
44 changes: 44 additions & 0 deletions app/src/main/java/com/example/Lesson5Activity.java
@@ -0,0 +1,44 @@
package com.example;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import com.example.dialog.MyDialogFragment;

/**
* ダイアログの作り方についてレッスン。
*/
public class Lesson5Activity extends AppCompatActivity implements MyDialogFragment.Callback {

private TextView mTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lesson5);

mTextView = findViewById(R.id.lesson5_text_view);
Button button = findViewById(R.id.lesson5_button);

button.setOnClickListener((l) -> onClick());
}

private void onClick() {
MyDialogFragment dialog = new MyDialogFragment();
dialog.show(getSupportFragmentManager(), MyDialogFragment.class.getSimpleName());
}

@Override
public void onMyDialogSucceeded(@NonNull String successText) {
mTextView.setText(successText);
}

@Override
public void onMyDialogCancelled(@NonNull String cancelText) {
mTextView.setText(cancelText);
}
}
59 changes: 59 additions & 0 deletions app/src/main/java/com/example/dialog/MyDialogFragment.java
@@ -0,0 +1,59 @@
package com.example.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

public class MyDialogFragment extends DialogFragment {
private Callback mCallback;

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Are you happy?")
.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mCallback.onMyDialogSucceeded("YES");
}
})
.setNegativeButton("no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mCallback.onMyDialogCancelled("NO");
}
});
return builder.create();
}

@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
Object callback = getParentFragment();
if (callback == null) {
callback = getActivity();
if (!(callback instanceof Callback)) {
throw new IllegalStateException();
}
}
mCallback = (Callback) callback;
}

@Override
public void onDetach() {
super.onDetach();
mCallback = null;
}

public interface Callback {
void onMyDialogSucceeded(@NonNull String successText);
void onMyDialogCancelled(@NonNull String cancelText);
}
}
29 changes: 29 additions & 0 deletions app/src/main/res/layout/activity_lesson5.xml
@@ -0,0 +1,29 @@
<?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=".Lesson5Activity">

<TextView
android:id="@+id/lesson5_text_view"
android:layout_width="59dp"
android:layout_height="20dp"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/lesson5_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.39" />

<Button
android:id="@+id/lesson5_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/lesson5_text_view" />
</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit fe5bf66

Please sign in to comment.