android第三次作业
1.返回键实现对话框弹出是否退出应用程序
1 package com.example.ggr; 2 3 import androidx.appcompat.app.AlertDialog; 4 import androidx.appcompat.app.AppCompatActivity; 5 6 import android.content.DialogInterface; 7 import android.os.Bundle; 8 9 public class MainActivity extends AppCompatActivity { 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_main); 15 16 } 17 18 @Override 19 public void onBackPressed() { 20 final AlertDialog dialog; 21 AlertDialog.Builder builder=new AlertDialog.Builder(this) 22 .setTitle("普通对话框")//设置对话框标题 23 .setIcon(R.mipmap.ic_launcher)//设置设置标题图标 24 .setMessage("是否确定退出应用:")//设置对话框的提示信息 25 //添加"确定"按钮 26 .setPositiveButton("确定", new DialogInterface.OnClickListener() { 27 @Override 28 public void onClick(DialogInterface dialog, int i) { 29 dialog.dismiss();//关闭对话框 30 MainActivity.this.finish();//关闭MainActivity 31 } 32 }) 33 //t添加"取消"按钮 34 .setNegativeButton("取消", new DialogInterface.OnClickListener() { 35 @Override 36 public void onClick(DialogInterface dialog, int i) { 37 dialog.dismiss(); 38 } 39 }); 40 dialog=builder.create(); 41 dialog.show(); 42 43 } 44 }
2.实现以下场景:从一个activity中点击一个按钮后,弹出一个单选按钮对话框,上面有“男”“女”两个选项,选定后,TOAST弹出 你选择了男,或你选择了女(参考书上改字体)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical"> 6 7 <TextView 8 android:id="@+id/tv" 9 android:gravity="center" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:layout_marginTop="20dp" 13 android:text="单选对话框" /> 14 15 <Button 16 android:onClick="onclick1" 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content" 19 android:layout_marginTop="20dp" 20 android:text="选择男女"/> 21 22 </LinearLayout>
1 package com.example.ggr; 2 3 4 import android.os.Bundle; 5 import android.app.Activity; 6 import android.app.AlertDialog; 7 import android.app.AlertDialog.Builder; 8 import android.content.DialogInterface; 9 import android.content.DialogInterface.OnClickListener; 10 import android.view.View; 11 import android.widget.Toast; 12 13 public class MainActivity extends Activity { 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 20 } 21 22 public void onclick1(View view) { 23 AlertDialog.Builder builder = new Builder(this); 24 builder.setIcon(android.R.drawable.ic_dialog_info); 25 builder.setTitle("请选择性别"); 26 final String[] items = new String[]{"男", "女"}; 27 builder.setSingleChoiceItems(items, -1, new OnClickListener() { 28 29 @Override 30 public void onClick(DialogInterface dialog, int which) { 31 Toast.makeText(MainActivity.this, "您选择的是:" + items[which], Toast.LENGTH_SHORT).show(); 32 dialog.dismiss(); 33 34 } 35 }); 36 builder.show(); 37 } 38 }
3.布局(详见:Android第五周上机word文档)
<?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" android:background="#81F781"> <TextView android:id="@+id/tv" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="1.textview显示文本信息" android:textColor="#0101DF" android:textSize="36dp"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="2.按钮" android:textColor="#0101DF" android:textSize="36dp"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:text="输入信息" android:textColor="#0101DF" android:textSize="36dp"/> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" android:textColor="#0101DF" android:textSize="36dp"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" android:textColor="#0101DF" android:textSize="36dp"/> <CheckBox android:layout_width="match_parent" android:layout_height="wrap_content" android:text="电脑" android:textColor="#0101DF" android:textSize="36dp"/> <CheckBox android:layout_width="match_parent" android:layout_height="wrap_content" android:text="手机" android:textColor="#0101DF" android:textSize="36dp"/> </LinearLayout>
4.教材p76页 图3—17购物商城界面
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical"> 6 7 <TextView 8 android:layout_width="match_parent" 9 android:layout_height="45dp" 10 android:text="购物商城" 11 android:textSize="18sp" 12 android:background="#FF8F03" 13 android:gravity="center"/> 14 <ListView 15 android:id="@+id/lv" 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content"/> 18 19 </LinearLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 6 <ImageView 7 android:layout_width="120dp" 8 android:layout_height="90dp" 9 android:id="@+id/iv" 10 android:layout_centerVertical="true"/> 11 <RelativeLayout 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:layout_marginLeft="10dp" 15 android:layout_toRightOf="@+id/iv" 16 android:layout_centerVertical="true"> 17 <TextView 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:id="@+id/title" 21 android:text="桌子" 22 android:textSize="20sp" 23 android:textColor="#000000" 24 /> 25 <TextView 26 android:layout_width="wrap_content" 27 android:layout_height="wrap_content" 28 android:id="@+id/tv_price" 29 android:text="价格" 30 android:layout_below="@id/title" 31 android:layout_marginTop="10dp" 32 android:textSize="20sp" 33 android:textColor="#FF8F03" 34 /> 35 <TextView 36 android:layout_width="wrap_content" 37 android:layout_height="wrap_content" 38 android:id="@+id/price" 39 android:text="1000" 40 android:textSize="20sp" 41 android:layout_below="@id/title" 42 android:layout_toRightOf="@id/tv_price" 43 android:layout_marginTop="10dp" 44 android:textColor="#FF8F03" 45 /> 46 </RelativeLayout> 47 48 49 </LinearLayout>
1 package com.example.ggr; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.view.ViewGroup; 7 import android.widget.BaseAdapter; 8 import android.widget.ImageView; 9 import android.widget.ListAdapter; 10 import android.widget.ListView; 11 import android.widget.TextView; 12 13 public class MainActivity extends AppCompatActivity { 14 private ListView mListView; 15 private String[] titles = {"桌子", "苹果", "蛋糕", "线衣", "猕猴桃", "围巾"}; 16 private String[] prices = {"1800元", "10/kg", "300元", "350元", "10/kg", "280元"}; 17 private int[] icons = {R.drawable.table, R.drawable.apple, R.drawable.cake,R.drawable.wrieclothes, R.drawable.kiwifruit, R.drawable.scarf}; 18 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23 mListView = (ListView) findViewById(R.id.lv); 24 MyBaseAdapter mAdapter = new MyBaseAdapter(); 25 mListView.setAdapter( mAdapter); 26 } 27 class MyBaseAdapter extends BaseAdapter { 28 29 @Override 30 public int getCount() { 31 return titles.length; 32 } 33 34 @Override 35 public Object getItem(int i) { 36 return titles[i]; 37 } 38 39 @Override 40 public long getItemId(int i) { 41 return i; 42 } 43 44 @Override 45 public View getView(int i, View view, ViewGroup viewGroup) { 46 view = View.inflate(MainActivity.this, R.layout.activity_main2, null); 47 TextView title = view.findViewById(R.id.title); 48 TextView price = view.findViewById(R.id.price); 49 ImageView iv = view.findViewById(R.id.iv); 50 title.setText(titles[i]); 51 price.setText(prices[i]); 52 iv.setImageResource(icons[i]); 53 return view; 54 } 55 } 56 }