第五六周作业
1.返回键实现对话框弹出是否退出应用程序
package com.example.a; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.AliasActivity; import android.app.Dialog; import android.content.DialogInterface; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onBackPressed(){ AlertDialog dialog; AlertDialog.Builder builder = new AlertDialog.Builder(this) .setIcon(R.drawable.ic_launcher) .setTitle("asd") .setMessage("sadsads") .setPositiveButton("quedig", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); MainActivity.this.finish(); } }) .setNegativeButton("quxiao", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout>
2.实现以下场景:从一个activity中点击一个按钮后,弹出一个单选按钮对话框,上面有“男”“女”两个选项,选定后,TOAST弹出 你选择了男,或你选择了女(参考书上改字体)
package com.example.a; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.AliasActivity; import android.app.Dialog; import android.content.DialogInterface; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onclick(View v){ AlertDialog dialog; AlertDialog.Builder builder = new AlertDialog.Builder(this) .setIcon(R.drawable.ic_launcher) .setTitle("选择") .setMessage("选择男女") .setPositiveButton("nan", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "你选择了男", 10).show(); dialog.dismiss(); } }) .setNegativeButton("nv", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "你选择了女", 10).show(); 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:layout_height="match_parent" android:layout_width="match_parent" android:text="按钮" android:onClick="onclick" /> </RelativeLayout>
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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="vertical"> <TextView android:layout_height="100dp" android:layout_width="match_parent" android:text="1.显示文本信息" /> <Button android:layout_height="100dp" android:layout_width="100dp" android:text="2.按钮" android:onClick="onclick" /> <EditText android:layout_height="100dp" android:layout_width="match_parent" android:hint="3.输入文本信息" /> <RadioGroup android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal"> <RadioButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="nan"/> <RadioButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="nv"/> </RadioGroup> <CheckBox android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="DIANNAO"/> <CheckBox android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="SHOUJI"/> </LinearLayout>
4.教材p76页 图3—17购物商城界面
package com.example.aswork1; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { List<Fruit> list = new ArrayList<Fruit>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initFruit();// 初始化数据源 ListView listview = (ListView) findViewById(R.id.listView1);// 获取界面上的Listview FruitAdapter adapter = new FruitAdapter(this, R.layout.activity_main2, list); listview.setAdapter(adapter); } // 定义数据源,使用集合的方式 public void initFruit() { Fruit apple = new Fruit("苹果", R.drawable.ic_launcher_background); Fruit cake = new Fruit("香蕉", R.drawable.ic_launcher_background); Fruit scarf = new Fruit("桃子", R.drawable.ic_launcher_background); Fruit table = new Fruit("梨", R.drawable.ic_launcher_background); list.add(apple); list.add(cake); list.add(scarf); list.add(table); } } package com.example.aswork1; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import java.util.List; public class FruitAdapter extends ArrayAdapter { int resourceid; public FruitAdapter(Context context, int resource, List objects) { super(context, resource, objects); // TODO Auto-generated constructor stub this.resourceid = resource; } @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { Fruit fruit = (Fruit) getItem(position);// 获取当前项的fruit实例 View view = LayoutInflater.from(getContext()).inflate(resourceid, null);// 实例化一个view,item对象 ImageView iv = (ImageView) view.findViewById(R.id.fruit_image);// 获取该布局内的Imageview TextView tv = (TextView) view.findViewById(R.id.fruit_name);// 获取该布局内的TextView iv.setImageResource(fruit.getImageid());//为图片视图设置图片资源 tv.setText(fruit.getName());//为文本视图设置文本内容 return view; } } package com.example.aswork1; public class Fruit { String name; int imageid; public Fruit(){ } public Fruit(String name, int imageid) { super(); this.name = name; this.imageid = imageid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getImageid() { return imageid; } public void setImageid(int imageid) { this.imageid = imageid; } }
<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=".MainActivity" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView> </RelativeLayout> <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" tools:context=".MainActivity" android:orientation="horizontal"> <ImageView android:id="@+id/fruit_image" android:layout_height="wrap_content" android:layout_width="wrap_content" /> <TextView android:id="@+id/fruit_name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>