Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
135 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
59
app/src/main/java/com/example/dialog/MyDialogFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |