第五六次上机作业
package itcast.cn; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.app.Activity; public class Main2Activity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); } public void onBackPressed(){ AlertDialog dialog; AlertDialog.Builder builder=new AlertDialog.Builder(this) .setTitle("普通对话框") .setIcon(R.drawable.ic_launcher_background) .setMessage("是否确定退出") .setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); Main2Activity.this.finish();} }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog=builder.create(); dialog.show(); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/bt_1" android:layout_width="wrap_content" android:layout_height="100dp" android:text="单选框" android:gravity="center" android:layout_marginTop="50dp" android:layout_gravity="center"> </Button> </LinearLayout>
package itcast.cn; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class Main2Activity extends Activity implements View.OnClickListener{ private int i=1; private int i1[]={1,2}; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); findViewById(R.id.bt_1).setOnClickListener(this); } public void onClick(View v){ AlertDialog dialog; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("你的性别是?"); builder.setSingleChoiceItems(new String[]{"女","男"},i,new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog,int which){ i=which; } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if (i==1){ Toast.makeText(Main2Activity.this,"你选择了男",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(Main2Activity.this,"你选择了女",Toast.LENGTH_SHORT).show(); } dialog.dismiss(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog = builder.create(); dialog.show();
package com.example.sanzhou; import android.net.wifi.WifiManager.MulticastLock; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.view.Menu; import android.view.MenuItem.OnMenuItemClickListener; import android.view.View; import android.widget.AbsListView.MultiChoiceModeListener; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onclick1(View view) { AlertDialog.Builder builder=new Builder(this); builder.setIcon(android.R.drawable.ic_dialog_info); builder.setTitle("请选择性别"); final String []items=new String[]{"男","女"}; builder.setSingleChoiceItems(items, -1, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "您选择的是:"+items[which], Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); builder.show(); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/tv" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="单选对话框" /> <Button android:onClick="onclick1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="选择男女"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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=".MainActivity" android:background="#8BA20B"> <TextView android:layout_width="wrap_content" android:layout_height="35dp" android:text="1.TextView显示文本信息" android:textStyle="italic" android:textColor="#E71D1D" android:layout_marginTop="20dp" android:layout_marginLeft="50dp" android:textSize="30sp"/> <Button android:layout_width="match_parent" android:layout_height="60dp" android:layout_marginLeft="20dp" android:layout_marginTop="60dp" android:layout_marginRight="20dp" android:text="2.按钮" android:textColor="#E71D1D" android:textSize="30sp" /> <EditText android:layout_width="match_parent" android:layout_height="60dp" android:text="3.编辑框:请输入信息" android:textSize="30sp" android:layout_marginTop="120dp" android:textColor="#E71D1D" android:layout_marginLeft="35dp"/> <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:orientation="horizontal"/> <RadioButton android:id="@+id/rb_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4.男" android:textSize="30sp" android:layout_marginTop="180dp" android:textColor="#E71D1D" android:layout_marginLeft="35dp"/> <RadioButton android:id="@+id/rb_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" android:layout_toRightOf="@id/rb_1" android:textSize="30sp" android:layout_marginTop="180dp" android:textColor="#E71D1D" android:layout_marginLeft="35dp"/> <CheckBox android:id="@+id/cb_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="电脑" android:textColor="#E71D1D" android:textSize="30sp" android:layout_marginTop="230dp" android:layout_marginLeft="35dp"/> <CheckBox android:id="@+id/cb_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="手机" android:textColor="#E71D1D" android:textSize="30sp" android:layout_below="@id/cb_1" android:layout_marginTop="10dp" android:layout_marginLeft="35dp"/> </RelativeLayo>