第五、六周作业
1.返回键实现对话框弹出是否退出应用程序
package com.example.myapplication; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface; import android.os.Bundle; public class Main6Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main6); } @Override public void onBackPressed() { final AlertDialog dialog; AlertDialog.Builder builder=new AlertDialog.Builder(this) .setTitle("普通对话框")//设置对话框标题 .setIcon(R.mipmap.ic_launcher)//设置设置标题图标 .setMessage("是否确定退出应用:")//设置对话框的提示信息 //添加"确定"按钮 .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int i) { dialog.dismiss();//关闭对话框 Main6Activity.this.finish();//关闭MainActivity } }) //t添加"取消"按钮 .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int i) { dialog.dismiss(); } }); dialog=builder.create(); dialog.show(); } }
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.布局
<?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=".Main7Activity" android:background="#4CAF50"> <TextView android:id="@+id/t_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="1.TixtView显示文本信息" android:textSize="25dp" android:textColor="#F81F1F" android:gravity="center"/> <Button android:id="@+id/bt1" android:layout_width="300dp" android:layout_height="wrap_content" android:text="2.按钮" android:textSize="25dp" android:textColor="#F81F1F" android:layout_below="@id/t_1" android:layout_centerHorizontal="true"/> <EditText android:id="@+id/e1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/bt1" android:hint="输入信息" android:maxLines="2" android:textSize="25dp"/> <RadioButton android:id="@+id/rbtn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25dp" android:text="男" android:layout_below="@id/e1"/> <RadioButton android:id="@+id/rbtn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25dp" android:text="女" android:layout_toRightOf="@id/rbtn1" android:layout_below="@id/e1" android:layout_marginLeft="5dp"/> <CheckBox android:id="@+id/cb1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/rbtn1" android:text="电脑" android:textSize="25dp"/> <CheckBox android:id="@+id/cb2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/cb1" android:text="手机" android:textSize="25dp"/> </RelativeLayout>
4.教材p76页 图3—17购物商城界面
<?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=".Main8Activity"> <TextView android:layout_width="match_parent" android:layout_height="45dp" android:text="购物商城" android:textSize="18sp" android:background="#FF8F03" android:gravity="center"/> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TextView; public class Main8Activity extends AppCompatActivity { private ListView mListView; private String[] titles = {"桌子", "苹果", "蛋糕", "线衣", "猕猴桃", "围巾"}; private String[] prices = {"1800元", "10/kg", "300元", "350元", "10/kg", "280元"}; private int[] icons = {R.drawable.table, R.drawable.apple, R.drawable.dangao, R.drawable.maoyi, R.drawable.mihoutao, R.drawable.weijin}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main8); mListView = (ListView) findViewById(R.id.lv); MyBaseAdapter mAdapter = new MyBaseAdapter(); mListView.setAdapter( mAdapter); } class MyBaseAdapter extends BaseAdapter { @Override public int getCount() { return titles.length; } @Override public Object getItem(int i) { return titles[i]; } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { view = View.inflate(Main8Activity.this, R.layout.list_item, null); TextView title = view.findViewById(R.id.title1); TextView price = view.findViewById(R.id.price); ImageView iv = view.findViewById(R.id.iv); title.setText(titles[i]); price.setText(prices[i]); iv.setImageResource(icons[i]); return view; } } }