package com.example.myapplication;
import android.app.AlertDialog;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.view.Menu;
public class Assignment03_02 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity03_02);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onBackPressed() {
AlertDialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("是否退出")
.setMessage("是否退出应用程序 :")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
Assignment03_02.this.finish();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
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"
tools:context=".Assignment03_02" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
package com.example.myapplication;
import android.app.AlertDialog;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Toast;
import android.widget.Button;
public class Assignment03_03 extends Activity {
int gender = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity03_03);
Button button0 = (Button)findViewById(R.id.button0);
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(Assignment03_03.this)
.setTitle("@string/chooseGender")
.setSingleChoiceItems(new String[]{ "男", "女" }, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
gender = which;
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Assignment03_03.this, "你选择了" + (gender == 0 ? "男" : "女"), Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
}
});
}
}
<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:orientation="vertical"
tools:context=".Assignment03_03" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chooseGender"
android:layout_marginTop="25dp"
android:layout_gravity="center"
/>
<Button
android:id="@+id/button0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/gender"
android:layout_marginTop="25dp"
android:layout_gravity="center"
/>
</LinearLayout>
package com.example.myapplication;
import android.os.Bundle;
import android.app.Activity;
public class Assignment03_04 extends Activity {
int gender = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity03_04);
}
}
<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:orientation="vertical"
tools:context=".Assignment03_04" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/textView"
android:layout_marginTop="25dp"
android:textSize="25sp"
android:gravity="center"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button"
android:layout_margin="25dp"
/>
<EditText
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/editText"
android:gravity="center"
/>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/radioButton0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="@string/male"
/>
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="@string/female"
/>
</RadioGroup>
<CheckBox
android:id="@+id/checkBox0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="@string/checkBox0"
/>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="@string/checkBox1"
/>
</LinearLayout>
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class Assignment03_01 extends AppCompatActivity {
private ListView mListView;
private String[] titles = { "桌子", "苹果", "蛋糕", "线衣", "猕猴桃", "围巾" };
private String[] prices = { "1800元", "10元/kg", "300元", "350元", "10元/kg", "280元" };
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 };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity03_01);
mListView = findViewById(R.id.listView);
MyBaseAdapter mAdapter = new MyBaseAdapter();
mListView.setAdapter(mAdapter);
}
class ViewHolder {
TextView textViewTitle;
TextView textViewContent;
ImageView imageView;
}
class MyBaseAdapter extends BaseAdapter {
@Override
public int getCount() {
return titles.length;
}
@Override
public Object getItem(int position) {
return titles[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = View.inflate(Assignment03_01.this, R.layout.list_item, null);
holder = new ViewHolder();
holder.textViewTitle = convertView.findViewById(R.id.textViewTitle);
holder.textViewContent = convertView.findViewById(R.id.textViewContent);
holder.imageView = convertView.findViewById(R.id.imageView);
}
else {
holder = (ViewHolder)convertView.getTag();
}
holder.textViewTitle.setText(titles[position]);
holder.textViewContent.setText(prices[position]);
holder.imageView.setBackgroundResource(icons[position]);
return convertView;
}
}
}
<?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="horizontal">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:paddingTop="25dp">
</TextView>
<TextView
android:id="@+id/textViewContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="11sp">
</TextView>
</LinearLayout>
</LinearLayout>