Android(安卓)studio Dialog 弹出式对话框
16lz
2021-12-04
文件结构
运行效果
主要代码
MainActivity
package cn.edu.sicnu.dialogdemo;import android.app.DialogFragment;import android.content.DialogInterface;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends AppCompatActivity implements MyInterface { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void showdialog1(View view){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("dialog 1"); builder.setMessage("please choose yes or no?"); builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(MainActivity.this, "yes",Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(MainActivity.this, "no",Toast.LENGTH_SHORT).show(); } }); builder.show(); } public void showdialog2(View view){ MyDialog myDialog = new MyDialog(); myDialog.show(getFragmentManager(),"dialog2"); } @Override public void buttonYesClicked() { Toast.makeText(MainActivity.this, "I am acticity , yes",Toast.LENGTH_SHORT).show(); } @Override public void buttonNoClicked() { Toast.makeText(MainActivity.this, "I am acticity , No",Toast.LENGTH_SHORT).show(); } public void showdialog3(View view){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("dialog 3"); final View v = getLayoutInflater().inflate(R.layout.dialoglayout,null); builder.setView(v); builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { EditText editText_username = v.findViewById(R.id.username); Toast.makeText(MainActivity.this, "username:"+ editText_username.getText(),Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(MainActivity.this, "no",Toast.LENGTH_SHORT).show(); } }); builder.show(); }}
MyDialog
package cn.edu.sicnu.dialogdemo;import android.annotation.SuppressLint;import android.app.Dialog;import android.app.DialogFragment;import android.content.DialogInterface;import android.os.Bundle;import android.support.v7.app.AlertDialog;import android.widget.Toast;public class MyDialog extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle("dialog 1"); builder.setMessage("please choose yes or no?"); builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { MyInterface myInterface = (MyInterface)getActivity(); myInterface.buttonYesClicked(); } }); builder.setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { MyInterface myInterface = (MyInterface)getActivity(); myInterface.buttonNoClicked(); } }); return builder.create(); }}
MyInterface
package cn.edu.sicnu.dialogdemo;public interface MyInterface { public void buttonYesClicked(); public void buttonNoClicked();}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.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="cn.edu.sicnu.dialogdemo.MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="2dp" android:onClick="showdialog1" android:text="Show Dialog" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="showdialog2" android:text="Show MyDialog" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/button" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="showdialog3" android:text="Show Login" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/button2" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/button3" />android.support.constraint.ConstraintLayout>
dialoglayout.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:hint="username?" android:text="" /> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="password?" android:inputType="textPassword" />LinearLayout>
更多相关文章
- Android抽屉效果(使用LayoutParams来移动View的位置)
- Android(安卓)自定义加载view(菊花效果)
- android 访问SQLite
- RecyclerView实现横向滚动效果
- Android应用开发——界面开发之常用组件的属性介绍
- android 动态效果学习之旅
- Android运行报错:Error: Static interface methods are only supp
- Android(安卓)剪切板操作
- Android(安卓)拖拽