Android第五、六周作业
1.返回键实现对话框弹出是否退出应用程序
1 package cn.itcast.myapplication; 2 3 import androidx.appcompat.app.ActionBar; 4 import androidx.appcompat.app.AlertDialog; 5 import androidx.appcompat.app.AppCompatActivity; 6 7 import android.content.DialogInterface; 8 import android.os.Bundle; 9 10 public class Main5Activity extends AppCompatActivity { 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_main5); 16 } 17 public void onBackPressed(){ 18 AlertDialog dialog; 19 AlertDialog.Builder builder=new AlertDialog.Builder(this); 20 builder.setTitle("普通对话框"); 21 builder.setIcon(R.mipmap.ic_launcher); 22 builder.setMessage("是否退出应用"); 23 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { 24 @Override 25 public void onClick(DialogInterface dialog,int which) { 26 dialog.dismiss(); 27 Main5Activity.this.finish(); 28 } 29 }); 30 31 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { 32 @Override 33 public void onClick(DialogInterface dialog,int which) { 34 dialog.dismiss(); 35 } 36 }); 37 dialog=builder.create(); 38 dialog.show(); 39 40 41 42 } 43 44 45 46 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_height="match_parent" 4 android:layout_width="match_parent"> 5 <Button 6 android:layout_width="wrap_content" 7 android:layout_height="wrap_content" 8 android:text="按钮1" 9 android:layout_centerInParent="true" 10 android:background="#777BB9"> 11 12 </Button> 13 </RelativeLayout>
2.实现以下场景:从一个activity中点击一个按钮后,弹出一个单选按钮对话框,上面有“男”“女”两个选项,选定后,TOAST弹出 你选择了男,或你选择了女(参考书上改字体那个案例)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 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 android:orientation="vertical" 8 tools:context=".Main3Activity"> 9 <TextView 10 android:id="@+id/tv" 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:text="您的性别是:" 14 android:gravity="center" 15 android:textSize="20sp" 16 android:layout_marginTop="10dp" 17 android:textColor="#FFFDB371"/> 18 <Button 19 android:id="@+id/bt" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:text="选择性别" 23 android:layout_gravity="center" 24 android:layout_marginTop="10dp"/> 25 26 </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文档)
1 package com.example.myapplication; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 6 public class Assignment03_04 extends Activity { 7 int gender = 0; 8 9 @Override 10 protected void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState); 12 setContentView(R.layout.activity03_04); 13 } 14 }
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context=".Assignment03_04" > 7 <TextView 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:text="@string/textView" 11 android:layout_marginTop="25dp" 12 android:textSize="25sp" 13 android:gravity="center" 14 /> 15 <Button 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content" 18 android:text="@string/button" 19 android:layout_margin="25dp" 20 /> 21 <EditText 22 android:inputType="text" 23 android:layout_width="match_parent" 24 android:layout_height="wrap_content" 25 android:hint="@string/editText" 26 android:gravity="center" 27 /> 28 <RadioGroup 29 android:id="@+id/radioGroup" 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:orientation="horizontal" 33 > 34 <RadioButton 35 android:id="@+id/radioButton0" 36 android:layout_width="wrap_content" 37 android:layout_height="wrap_content" 38 android:textSize="25sp" 39 android:text="@string/male" 40 /> 41 <RadioButton 42 android:id="@+id/radioButton1" 43 android:layout_width="wrap_content" 44 android:layout_height="wrap_content" 45 android:textSize="25sp" 46 android:text="@string/female" 47 /> 48 </RadioGroup> 49 <CheckBox 50 android:id="@+id/checkBox0" 51 android:layout_width="wrap_content" 52 android:layout_height="wrap_content" 53 android:textSize="25sp" 54 android:text="@string/checkBox0" 55 /> 56 <CheckBox 57 android:id="@+id/checkBox1" 58 android:layout_width="wrap_content" 59 android:layout_height="wrap_content" 60 android:textSize="25sp" 61 android:text="@string/checkBox1" 62 /> 63 64 </LinearLayout>
4.
1 package com.example.myapplication; 2 3 import android.os.Bundle; 4 import android.view.View; 5 import android.view.ViewGroup; 6 import android.widget.BaseAdapter; 7 import android.widget.ImageView; 8 import android.widget.ListView; 9 import android.widget.TextView; 10 11 import androidx.appcompat.app.AppCompatActivity; 12 13 public class Assignment03_01 extends AppCompatActivity { 14 private ListView mListView; 15 private String[] titles = { "桌子", "苹果", "蛋糕", "线衣", "猕猴桃", "围巾" }; 16 private String[] prices = { "1800元", "10元/kg", "300元", "350元", "10元/kg", "280元" }; 17 18 private int[] icons = { R.drawable.ic_launcher_foreground, R.drawable.ic_launcher_foreground, R.drawable.ic_launcher_foreground, R.drawable.ic_launcher_foreground, R.drawable.ic_launcher_foreground, R.drawable.ic_launcher_foreground }; 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity03_01); 24 mListView = findViewById(R.id.listView); 25 MyBaseAdapter mAdapter = new MyBaseAdapter(); 26 mListView.setAdapter(mAdapter); 27 } 28 class ViewHolder { 29 TextView textViewTitle; 30 TextView textViewContent; 31 ImageView imageView; 32 } 33 class MyBaseAdapter extends BaseAdapter { 34 @Override 35 public int getCount() { 36 return titles.length; 37 } 38 @Override 39 public Object getItem(int position) { 40 return titles[position]; 41 } 42 @Override 43 public long getItemId(int position) { 44 return position; 45 } 46 @Override 47 public View getView(int position, View convertView, ViewGroup parent) { 48 ViewHolder holder = null; 49 if (convertView == null) { 50 convertView = View.inflate(Assignment03_01.this, R.layout.list_item, null); 51 holder = new ViewHolder(); 52 holder.textViewTitle = convertView.findViewById(R.id.textViewTitle); 53 holder.textViewContent = convertView.findViewById(R.id.textViewContent); 54 holder.imageView = convertView.findViewById(R.id.imageView); 55 } 56 else { 57 holder = (ViewHolder)convertView.getTag(); 58 } 59 holder.textViewTitle.setText(titles[position]); 60 holder.textViewContent.setText(prices[position]); 61 holder.imageView.setBackgroundResource(icons[position]); 62 return convertView; 63 } 64 } 65 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="horizontal"> 7 <ImageView 8 android:id="@+id/imageView" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content"> 11 12 </ImageView> 13 <LinearLayout 14 android:layout_width="wrap_content" 15 android:layout_height="match_parent" 16 android:orientation="vertical"> 17 <TextView 18 android:id="@+id/textViewTitle" 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:textSize="16sp" 22 android:paddingTop="25dp"> 23 </TextView> 24 <TextView 25 android:id="@+id/textViewContent" 26 android:layout_width="wrap_content" 27 android:layout_height="wrap_content" 28 android:textSize="11sp"> 29 </TextView> 30 </LinearLayout> 31 32 </LinearLayout>