Android第五、六周作业

1.返回键实现对话框弹出是否退出应用程序

 1 package com.example.android_924;
 2 
 3 import androidx.appcompat.app.AlertDialog;
 4 import androidx.appcompat.app.AppCompatActivity;
 5 import android.content.DialogInterface;
 6 import android.os.Bundle;
 7 public class MainActivity extends AppCompatActivity {
 8     @Override
 9     protected void onCreate(Bundle savedInstanceState) {
10         super.onCreate(savedInstanceState);
11         setContentView(R.layout.activity_main);
12     }
13     @Override
14     public void onBackPressed(){
15         final AlertDialog dialog;
16         AlertDialog.Builder builder=new AlertDialog.Builder(this)
17                 .setTitle("普通对话框")
18                 .setIcon(R.mipmap.ic_launcher)
19                 .setMessage("是否确定退出应用:")
20                 .setPositiveButton("是", new DialogInterface.OnClickListener() {
21                     @Override
22                     public void onClick(DialogInterface dialog, int which) {
23                         dialog.dismiss();
24                         MainActivity.this.finish();
25                     }
26                 })
27                 .setNegativeButton("否", new DialogInterface.OnClickListener() {
28                     @Override
29                     public void onClick(DialogInterface dialog, int which) {
30                         dialog.dismiss();
31                     }
32                 });
33         dialog=builder.create();
34         dialog.show();
35 
36     }
37 }

 

 

 2.实现以下场景:从一个activity中点击一个按钮后,弹出一个单选按钮对话框,上面有“男”“女”两个选项,选定后,TOAST弹出 你选择了男,或你选择了女(参考书上改字体)

 1 import android.app.Activity;
 2 import android.app.AlertDialog;
 3 import android.app.AlertDialog.Builder;
 4 import android.content.DialogInterface;
 5 import android.view.Menu;
 6 import android.view.View;
 7 import android.view.View.OnClickListener;
 8 import android.widget.Button;
 9 import android.widget.Toast;
10 
11 public class MainActivity extends Activity implements OnClickListener {
12 
13     private int num=0;
14     private int num1[]={0,1};
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_main);
19         findViewById(R.id.bt).setOnClickListener(this);
20     }
21     @Override
22     public void onClick(View v) {
23         // TODO Auto-generated method stub
24         AlertDialog.Builder builder=new AlertDialog.Builder(this)
25                 .setTitle("性别")
26                 .setSingleChoiceItems(new String[]{"男","女"}, -1, new DialogInterface.OnClickListener() {
27 
28                     @Override
29                     public void onClick(DialogInterface dialog, int which) {
30                         // TODO Auto-generated method stub
31                         num=which;
32                     }
33                 })
34                 .setPositiveButton("确定",new DialogInterface.OnClickListener() {
35                     @Override
36                     public void onClick(DialogInterface dialog, int which) {
37                         // TODO Auto-generated method stub
38                         if(num==0){
39                             Toast.makeText(MainActivity.this, "你选择的是男", Toast.LENGTH_LONG).show();
40                         }else{
41                             Toast.makeText(MainActivity.this, "你选择的是女", Toast.LENGTH_SHORT).show();
42                         }
43                         dialog.dismiss();
44                     }
45                 })
46                 .setNegativeButton("取消", new DialogInterface.OnClickListener() {
47 
48                     @Override
49                     public void onClick(DialogInterface dialog, int which) {
50                         // TODO Auto-generated method stub
51                         dialog.dismiss();
52                     }
53                 });
54         AlertDialog dialog=builder.create();
55         dialog.show();
56     }
57 }
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context=".MainActivity">
 8 
 9     <Button
10         android:id="@+id/bt "
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:text="请选择你的性别"
14 />
15 </RelativeLayout>

 

 

 3.布局(详见:Android第五周上机word文档)

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context=".MainActivity"
 8     android:background="#67AC96">
 9 
10     <TextView
11         android:layout_width="wrap_content"
12         android:layout_height="40dp"
13         android:text="1.TextView显示文本信息"
14         android:textStyle="italic"
15         android:textColor="#FF0000"
16         android:layout_marginTop="10dp"
17         android:layout_marginLeft="50dp"
18         android:textSize="30sp"/>
19 
20     <Button
21         android:layout_width="match_parent"
22         android:layout_height="60dp"
23         android:layout_marginLeft="20dp"
24         android:layout_marginTop="60dp"
25         android:layout_marginRight="20dp"
26         android:text="2.按钮"
27         android:background="@drawable/glg"
28         android:textColor="#FF0000"
29         android:textSize="30sp" />
30     <EditText
31         android:layout_width="match_parent"
32         android:layout_height="60dp"
33         android:text="3.编辑框:请输入信息"
34         android:textSize="30sp"
35         android:layout_marginTop="120dp"
36         android:textColor="#FF0000"
37         android:layout_marginLeft="35dp"/>
38     <RadioGroup
39         android:layout_width="wrap_content"
40         android:layout_height="wrap_content"
41         android:layout_marginLeft="20dp"
42         android:layout_marginRight="20dp"
43         android:orientation="horizontal"/>
44     <RadioButton
45         android:id="@+id/rb1"
46         android:layout_width="wrap_content"
47         android:layout_height="wrap_content"
48         android:text="4.男"
49         android:textSize="30sp"
50         android:layout_marginTop="180dp"
51         android:textColor="#FF0000"
52         android:layout_marginLeft="35dp"/>
53     <RadioButton
54         android:id="@+id/rb2"
55         android:layout_width="wrap_content"
56         android:layout_height="wrap_content"
57         android:text="女"
58         android:layout_toRightOf="@id/rb1"
59         android:textSize="30sp"
60         android:layout_marginTop="180dp"
61         android:textColor="#FF0000"
62         android:layout_marginLeft="35dp"/>
63     <CheckBox
64         android:id="@+id/cb1"
65         android:layout_width="wrap_content"
66         android:layout_height="wrap_content"
67         android:text="电脑"
68         android:textColor="#FF0000"
69         android:textSize="30sp"
70         android:layout_marginTop="230dp"
71         android:layout_marginLeft="35dp"/>
72     <CheckBox
73         android:id="@+id/cb2"
74         android:layout_width="wrap_content"
75         android:layout_height="wrap_content"
76         android:text="手机"
77         android:textColor="#FF0000"
78         android:textSize="30sp"
79         android:layout_below="@id/cb1"
80         android:layout_marginTop="10dp"
81         android:layout_marginLeft="35dp"/>
82 
83 
84 </RelativeLayout>

 

 4.教材p76页 图3—17购物商城界面

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context=".MainActivity">
 8 
 9     <TextView
10         android:id="@+id/gouqu"
11         android:layout_width="match_parent"
12         android:layout_height="50dp"
13         android:background="#4CE713"
14         android:text="购物商城"
15         android:textSize="35sp"
16         android:gravity="center"
17         />
18     <ListView
19         android:layout_width="match_parent"
20         android:layout_height="match_parent"
21         android:id="@+id/lv"
22         android:layout_below="@id/gouqu"/>
23 </RelativeLayout>
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5     <ImageView
 6         android:layout_width="200dp"
 7         android:layout_height="200dp"
 8         android:id="@+id/shop_iv"
 9         />
10     <RelativeLayout
11         android:layout_width="match_parent"
12         android:layout_height="match_parent"
13         android:layout_toRightOf="@id/shop_iv"
14         >
15         <TextView
16             android:layout_width="wrap_content"
17             android:layout_height="wrap_content"
18             android:id="@+id/name_shop"
19             android:layout_marginTop="50dp"
20             android:layout_marginLeft="15dp"
21             />
22         <TextView
23             android:layout_width="wrap_content"
24             android:layout_height="wrap_content"
25             android:text="价格:"
26             android:id="@+id/pr"
27             android:layout_below="@id/name_shop"
28             android:layout_marginTop="50dp"
29             android:layout_marginLeft="15dp"/>
30         <TextView
31             android:layout_width="wrap_content"
32             android:layout_height="wrap_content"
33             android:id="@+id/price_shop"
34             android:layout_toRightOf="@id/pr"
35             android:layout_below="@id/name_shop"
36             android:layout_marginTop="50dp"
37             android:layout_marginLeft="15dp"/>
38     </RelativeLayout>
39 </RelativeLayout>
package com.example.myapplication924_04;
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 MainActivity 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.cake, R.drawable.wireclothes, R.drawable.kiwifruit, R.drawable.scarf};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        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(MainActivity.this, R.layout.item, null);
            TextView title = view.findViewById(R.id.name_shop);
            TextView price = view.findViewById(R.id.price_shop);
            ImageView iv = view.findViewById(R.id.shop_iv);
            title.setText(titles[i]);
            price.setText(prices[i]);
            iv.setImageResource(icons[i]);
            return view;
        }
    }

}

 

posted @ 2021-09-25 13:05  韩式小火锅  阅读(15)  评论(0编辑  收藏  举报