第五、六周作业
1.返回键实现对话框弹出是否退出应用程序
package com.example.myapplication; import android.app.AlertDialog; import android.os.Bundle; import android.app.Activity; import android.content.DialogInterface; import android.view.Menu; public class Assignment03_02 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity03_02); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. // getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onBackPressed() { AlertDialog dialog; AlertDialog.Builder builder = new AlertDialog.Builder(this) .setTitle("是否退出") .setMessage("是否退出应用程序 :") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); Assignment03_02.this.finish(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog = builder.create(); dialog.show(); } }
<RelativeLayout 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" tools:context=".Assignment03_02" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout>
2.实现以下场景:从一个activity中点击一个按钮后,弹出一个单选按钮对话框,上面有“男”“女”两个选项,选定后,TOAST弹出 你选择了男,或你选择了女(参考书上改字体)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context=".Main3Activity"> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="您的性别是:" android:gravity="center" android:textSize="20sp" android:layout_marginTop="10dp" android:textColor="#FFFDB371"/> <Button android:id="@+id/bt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选择性别" android:layout_gravity="center" android:layout_marginTop="10dp"/> </LinearLayout>
package com.example.myapplication; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class Main3Activity extends AppCompatActivity implements View.OnClickListener { private TextView textView; private String[] sexArry= {"男", "女"}; int sex = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); findViewById(R.id.bt).setOnClickListener(this); textView = (TextView) findViewById(R.id.tv); } @Override public void onClick(View v) { AlertDialog dialog; AlertDialog.Builder builder=new AlertDialog.Builder(this) .setTitle("选择性别") .setIcon(R.mipmap.ic_launcher) .setSingleChoiceItems(new String[]{"男", "女"}, sex, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int i) { sex=i; } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int i) { dialog.dismiss();//关闭对话框 if (sex==1){ textView.setText("您的性别是:女"); }else{ textView.setText("您的性别是:男"); } } }) //t添加"取消"按钮 .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int i) { dialog.dismiss(); } }); dialog=builder.create(); dialog.show(); } }
3.布局(详见:Android第五周上机word文档)
<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:orientation="vertical" tools:context=".Assignment03_04" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/textView" android:layout_marginTop="25dp" android:textSize="25sp" android:gravity="center" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/button" android:layout_margin="25dp" /> <EditText android:inputType="text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/editText" android:gravity="center" /> <RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/radioButton0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25sp" android:text="@string/male" /> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25sp" android:text="@string/female" /> </RadioGroup> <CheckBox android:id="@+id/checkBox0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25sp" android:text="@string/checkBox0" /> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25sp" android:text="@string/checkBox1" /> </LinearLayout>
4.教材p76页 图3—17购物商城界面