布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:orientation="vertical" android:paddingTop="@dimen/activity_vertical_margin" tools:context="helloworld.com.inspur.demo8.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bt1" android:text="提交"/>] <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bt2" android:text="提交"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bt3" android:text="提交"/>] <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bt4" android:text="提交"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_show"/> </LinearLayout>
逻辑
package helloworld.com.inspur.demo8; import android.content.DialogInterface; import android.preference.MultiSelectListPreference; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.ScrollingTabContainerView; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; import java.util.jar.Manifest; public class MainActivity extends AppCompatActivity { private Button bt1,bt2,bt3,bt4; String info=""; private TextView tv_show; private String[] args={"java","C++","python"}; private boolean[] isC={false,false,false}; List<String> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt1= (Button)findViewById(R.id.bt1); bt2=(Button)findViewById(R.id.bt2); bt3= (Button)findViewById(R.id.bt3); bt4=(Button)findViewById(R.id.bt4); tv_show=(TextView)findViewById(R.id.tv_show); list=new ArrayList<String>(); bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog dia= new AlertDialog.Builder(MainActivity.this) .setTitle("hahahha") .setIcon(R.mipmap.ic_launcher) .setItems(args, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { info=args[which]; AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setPositiveButton("确认", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { tv_show.setText(info); } }); builder.create().show(); } }) .create(); dia.show(); } }); bt2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog dia= new AlertDialog.Builder(MainActivity.this) .setTitle("复选框") .setIcon(R.mipmap.ic_launcher) .setMultiChoiceItems(args, isC, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if(isChecked){ list.add(args[which]); } else{ list.remove(args[which]); } } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { tv_show.setText(list.toString()); } }) .create(); dia.show(); } }); bt3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ImageView image = new ImageView(MainActivity.this); image.setImageResource(R.mipmap.ic_launcher); AlertDialog dia= new AlertDialog.Builder(MainActivity.this) .setTitle("image") .setIcon(R.mipmap.ic_launcher) .setView(image) .setPositiveButton("确定",null) .setNegativeButton("取消",null) .create(); dia.show(); } }); bt4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog dia= new AlertDialog.Builder(MainActivity.this) .setTitle("single") .setIcon(R.mipmap.ic_launcher) .setSingleChoiceItems(args, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { info=args[which]; } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { tv_show.setText(info); } }) .create(); dia.show(); } }); } }