package com.example.myapplication; import android.content.DialogInterface; import android.os.Bundle; import android.support.design.widget.BottomNavigationView; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.support.annotation.NonNull; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import com.example.myapplication.utils.ToastTool; public class AlertDialog2Activity extends AppCompatActivity { private TextView mTextMessage; private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.navigation_home: mTextMessage.setText(R.string.title_home); return true; case R.id.navigation_dashboard: mTextMessage.setText(R.string.title_dashboard); return true; case R.id.navigation_notifications: mTextMessage.setText(R.string.title_notifications); return true; } return false; } }; private Button button1, button2, button3, button4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_alert_dialog2); BottomNavigationView navView = findViewById(R.id.nav_view); mTextMessage = findViewById(R.id.message); navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); button1 = findViewById(R.id.save_scale_type1); button2 = findViewById(R.id.save_scale_type2); button3 = findViewById(R.id.save_scale_type3); button4 = findViewById(R.id.save_scale_type4); OnClick onClick = new OnClick(); button1.setOnClickListener(onClick); button2.setOnClickListener(onClick); button3.setOnClickListener(onClick); button4.setOnClickListener(onClick); } class OnClick implements View.OnClickListener { @Override public void onClick(View v) { switch (v.getId()) { case R.id.save_scale_type1: AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialog2Activity.this); builder.setTitle("请回答").setMessage("cyc是人吗").setIcon(R.drawable.login).setPositiveButton("是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ToastTool.showMsg(AlertDialog2Activity.this, "是的"); } }).setNegativeButton("好像是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ToastTool.showMsg(AlertDialog2Activity.this, "肯定句,重新回答"); } }).setNeutralButton("不是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ToastTool.showMsg(AlertDialog2Activity.this, "弄死你"); } }).show(); break; case R.id.save_scale_type2: final String[] array2 = new String[]{"nan", "nv"}; AlertDialog.Builder builder2 = new AlertDialog.Builder(AlertDialog2Activity.this); builder2.setTitle("选择性别").setSingleChoiceItems(array2,1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ToastTool.showMsg(AlertDialog2Activity.this, array2[which]); dialog.dismiss(); } }).setCancelable(false).show(); break; case R.id.save_scale_type3: final String[] nametype = new String[]{"java", "C#", "python"}; final boolean[] booltype = new boolean[]{false, false, true}; AlertDialog.Builder builder3 = new AlertDialog.Builder(AlertDialog2Activity.this); builder3.setTitle("选择兴趣").setMultiChoiceItems(nametype, booltype, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { ToastTool.showMsg(AlertDialog2Activity.this, nametype[which] + ":" + isChecked); } }).setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).show(); break; case R.id.save_scale_type4: AlertDialog.Builder builder4 = new AlertDialog.Builder(AlertDialog2Activity.this); View view = LayoutInflater.from(AlertDialog2Activity.this).inflate(R.layout.activity_alert_dislog_login, null); final EditText username = view.findViewById(R.id.dialog_username); final EditText password = view.findViewById(R.id.dialog_password); Button login = view.findViewById(R.id.dialog_lonig); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { System.out.println("username:" + username); System.out.println("username:" + username.getText()); System.out.println("username:" + username.getText().toString()); String user = username.getText().toString(); if (user.equals("cyc")) { ToastTool.showMsg(AlertDialog2Activity.this, "登录成功"); } else { ToastTool.showMsg(AlertDialog2Activity.this, "登录失败"); } } }); builder4.setTitle("请登录").setView(view).show(); break; } } } }
下面的页面代码
<?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:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".AlertDialog2Activity"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/save_scale_type1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="stype1"/> <Button android:id="@+id/save_scale_type2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="stype2"/> <Button android:id="@+id/save_scale_type3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="stype3"/> <Button android:id="@+id/save_scale_type4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="stype4"/> </LinearLayout> <android.support.design.widget.BottomNavigationView android:id="@+id/nav_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="0dp" android:layout_marginEnd="0dp" android:background="?android:attr/windowBackground" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:menu="@menu/bottom_nav_menu" /> </android.support.constraint.ConstraintLayout>