xml的代码如下,用于编写按钮:
xmlversion=”1.0″encoding=”utf-8″?><LinearLayoutxmlns:android=”http://schemas.android.com/apk/res/android”android:layout_width=”match_parent”android:layout_height=”match_parent”android:background=”@drawable/background”xmlns:widget=”http://schemas.android.com/apk/res-auto”android:orientation=”vertical”><Buttonandroid:id=”@+id/button_1″android:layout_width=”match_parent”android:layout_height=”wrap_content”android:text=”简单的dialog”/><Buttonandroid:id=”@+id/button_2″android:layout_width=”match_parent”android:layout_height=”wrap_content”android:text=”列表的dialog”/><Buttonandroid:id=”@+id/button_3″android:layout_width=”match_parent”android:layout_height=”wrap_content”android:text=”单选的dialog”/><Buttonandroid:id=”@+id/button_4″android:layout_width=”match_parent”android:layout_height=”wrap_content”android:text=”多选的dialog”/>LinearLayout>
Java代码如下,用于完成逻辑:
importandroidx.appcompat.app.ActionBar;importandroidx.appcompat.app.AlertDialog;importandroidx.appcompat.app.AppCompatActivity;importandroid.content.DialogInterface;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.Toast;publicclassMainActivityextendsAppCompatActivity{intindex;
String[]item={“Android”,”IOS”,”Spark”,”Hadoop”,”Web”};boolean[]bools={false,false,false,false,false};//设置boolean数组一切的选项设置默许没选@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBaractionBar=getSupportActionBar();if(actionBar!=null){
actionBar.hide();
}
Buttonbutton=(Button)findViewById(R.id.button_1);
button.setOnClickListener(newView.OnClickListener(){
@OverridepublicvoidonClick(Viewview){
AlertDialog.Builderbuilder=newAlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.girl);
builder.setTitle(“标题栏”);
builder.setMessage(“对话框内容,可自行设置”);
builder.setPositiveButton(“确认”,newDialogInterface.OnClickListener(){
@OverridepublicvoidonClick(DialogInterfacedialog,intwhich){
Toast.makeText(MainActivity.this,”点击了确认”,Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton(“撤销”,newDialogInterface.OnClickListener(){
@OverridepublicvoidonClick(DialogInterfacedialogInterface,inti){
Toast.makeText(MainActivity.this,”点击了撤销”,Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton(“好的”,newDialogInterface.OnClickListener(){
@OverridepublicvoidonClick(DialogInterfacedialogInterface,inti){
Toast.makeText(MainActivity.this,”点击了“好的””,Toast.LENGTH_SHORT).show();
}
});
AlertDialogalertDialog=builder.create();
alertDialog.show();
}
});
Buttonbutton2=(Button)findViewById(R.id.button_2);
button2.setOnClickListener(newView.OnClickListener(){
@OverridepublicvoidonClick(Viewview){
AlertDialog.Builderbuilder=newAlertDialog.Builder(MainActivity.this);
builder.setTitle(“请挑选一个技能分支”);
builder.setItems(item,newDialogInterface.OnClickListener(){
@OverridepublicvoidonClick(DialogInterfacedialog,intwhich){
Toast.makeText(MainActivity.this,”挑选了”+item[which],Toast.LENGTH_SHORT).show();
}
});//撤销能够不增加//builder.setNegativeButton(“撤销”,null);AlertDialogalertDialog=builder.create();
alertDialog.show();
}
});
Buttonbutton3=(Button)findViewById(R.id.button_3);
button3.setOnClickListener(newView.OnClickListener(){
@OverridepublicvoidonClick(Viewview){
AlertDialog.Builderbuilder=newAlertDialog.Builder(MainActivity.this);
builder.setTitle(“请挑选技能分支:”);
builder.setSingleChoiceItems(item,index,newDialogInterface.OnClickListener(){
@OverridepublicvoidonClick(DialogInterfacedialog,intwhich){
index=which;
}
});
builder.setPositiveButton(“确认”,newDialogInterface.OnClickListener(){
@OverridepublicvoidonClick(DialogInterfacedialog,intwhich){
Toast.makeText(MainActivity.this,”挑选了”+item[index],Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton(“撤销”,null);
AlertDialogalertDialog=builder.create();
alertDialog.show();
}
});
Buttonbutton4=(Button)findViewById(R.id.button_4);
button4.setOnClickListener(newView.OnClickListener(){
@OverridepublicvoidonClick(Viewview){
AlertDialog.Builderbuilder=newAlertDialog.Builder(MainActivity.this);
builder.setTitle(“请挑选技能分支:”);
builder.setMultiChoiceItems(item,bools,newDialogInterface.OnMultiChoiceClickListener(){
@OverridepublicvoidonClick(DialogInterfacedialog,intwhich,booleanisChecked){
bools[which]=isChecked;
}
});
builder.setPositiveButton(“确认”,newDialogInterface.OnClickListener(){
@OverridepublicvoidonClick(DialogInterfacedialog,intwhich){
StringBuffersb=newStringBuffer();for(inti=0;i<item.length;i++){if(bools[i]){
sb.append(item[i]+””);
}
}
Toast.makeText(MainActivity.this,”挑选了”+sb.toString(),Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton(“撤销”,null);
AlertDialogalertDialog=builder.create();
alertDialog.show();
}
});
}
}
1、IT大王遵守相关法律法规,由于本站资源全部来源于网络程序/投稿,故资源量太大无法一一准确核实资源侵权的真实性;
2、出于传递信息之目的,故IT大王可能会误刊发损害或影响您的合法权益,请您积极与我们联系处理(所有内容不代表本站观点与立场);
3、因时间、精力有限,我们无法一一核实每一条消息的真实性,但我们会在发布之前尽最大努力来核实这些信息;
4、无论出于何种目的要求本站删除内容,您均需要提供根据国家版权局发布的示范格式
《要求删除或断开链接侵权网络内容的通知》:https://itdw.cn/ziliao/sfgs.pdf,
国家知识产权局《要求删除或断开链接侵权网络内容的通知》填写说明: http://www.ncac.gov.cn/chinacopyright/contents/12227/342400.shtml
未按照国家知识产权局格式通知一律不予处理;请按照此通知格式填写发至本站的邮箱 wl6@163.com