Android小项目实现

实验内容: 1. 利用自动完成文本功能设计一个应用程序,要求在输入课程名称时能够显示相应的提示。

2. 音频播放器,要求至少有播放、暂停和停止三个图像按钮。

3. 利用List View控件设计一个通讯录,要求每条信息包含头像、姓名和电话号码三部分内容。

4. 利用Grid View控件设计一个展示图片的应用程序,要求以九宫格形式展示图片,并且每张照片下面要加说明文字。

答题区

  • 利用自动完成文本功能设计一个应用程序,要求在输入课程名称时能够显示相应的提示。

以下是几个需要修改的文件代码

程序代码:

MainActivity.java

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);

        String[] courses = {
"math", "english", "physical", "chemistry", "blog", "computer"};

        ArrayAdapter<String> adapter =
new ArrayAdapter<>(this,
                android.R.layout.
simple_dropdown_item_1line, courses);

        AutoCompleteTextView textView = findViewById(R.id.
favorite_course);
        textView.setAdapter(adapter);

        MultiAutoCompleteTextView multiTextView = findViewById(R.id.
multi_favorite_courses);
        multiTextView.setAdapter(adapter);
        multiTextView.setTokenizer(
new MultiAutoCompleteTextView.CommaTokenizer());
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:fillViewport="true"

    tools:context=".MainActivity">



    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="vertical"

        android:padding="16dp">



        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="学生调查问卷app"

            android:textSize="24sp"

            android:textStyle="bold" />



        <EditText

            android:id="@+id/name"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:hint="姓名" />



        <EditText

            android:id="@+id/class1"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:hint="班级" />



        <EditText

            android:id="@+id/student_id"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:hint="学号"

            android:inputType="number" />



        <RadioGroup

            android:id="@+id/gender_group"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:orientation="horizontal">



            <RadioButton

                android:id="@+id/male"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="" />



            <RadioButton

                android:id="@+id/female"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="" />

        </RadioGroup>



        <AutoCompleteTextView

            android:id="@+id/favorite_course"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:hint="喜爱的课程" />

        <MultiAutoCompleteTextView

            android:id="@+id/multi_favorite_courses"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:hint="多个喜爱的课程(用逗号分隔)"

            android:inputType="text" />

        <Button

            android:id="@+id/submit"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="提交" />



    </LinearLayout>
</ScrollView>


 

二.音频播放器,要求至少有播放、暂停和停止三个图像按钮。

以下是几个需要修改的文件代码

程序代码:

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;



import android.media.MediaPlayer;

import android.os.Bundle;

import android.view.View;

import android.widget.SeekBar;

import android.widget.TextView;



public class MainActivity extends AppCompatActivity {



    private MediaPlayer mediaPlayer;

    private SeekBar seekBar; // declare a seekbar object

    private TextView textView; // declare a textview object



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        // initialize the media player with the raw audio file

        mediaPlayer = MediaPlayer.create(this, R.raw.aini_live);



        // find the seekbar and textview by their ids

        seekBar = findViewById(R.id.seekBar);

        textView = findViewById(R.id.textView);



        // set the maximum value of the seekbar to the duration of the audio file

        seekBar.setMax(mediaPlayer.getDuration());



        // set a listener on the seekbar to track the user's progress

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override

            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                // if the user is dragging the thumb, update the media player position and the textview

                if (fromUser) {

                    mediaPlayer.seekTo(progress);

                    textView.setText(formatTime(progress));

                }

            }



            @Override

            public void onStartTrackingTouch(SeekBar seekBar) {

                // pause the media player when the user starts dragging the thumb

                mediaPlayer.pause();

            }



            @Override

            public void onStopTrackingTouch(SeekBar seekBar) {

                // resume the media player when the user stops dragging the thumb

                mediaPlayer.start();

            }

        });



        // set a listener on the media player to update the seekbar and textview as the audio plays

        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

            @Override

            public void onCompletion(MediaPlayer mp) {

                // reset the seekbar and textview when the audio is finished

                seekBar.setProgress(0);

                textView.setText("00:00");

            }

        });

    }



    // play the audio file

    public void play(View view) {

        // check if mediaPlayer is null

        if (mediaPlayer == null) {

            // create a new mediaPlayer object

            mediaPlayer = MediaPlayer.create(this, R.raw.aini_live);

        }

        mediaPlayer.start();

    }



    // pause the audio file

    public void pause(View view) {

        mediaPlayer.pause();

    }



    // stop the audio file and reset the media player

    public void stop(View view) {

        mediaPlayer.stop();

        mediaPlayer.reset();

        mediaPlayer.release();

        mediaPlayer = null;

    }



    // format the time in milliseconds to minutes and seconds

    private String formatTime(int millis) {

        int minutes = millis / 1000 / 60;

        int seconds = millis / 1000 % 60;

        return String.format("%02d:%02d", minutes, seconds);

    }

}
activty_main.xml
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingLeft="16dp"

    android:paddingRight="16dp" >



    <ImageView

        android:id="@+id/image"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:src="@drawable/wang" />



    <LinearLayout

        android:id="@+id/buttons"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_below="@id/image"

        android:orientation="horizontal" >



        <Button

            android:id="@+id/play"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:background="@android:drawable/ic_media_play"

            android:onClick="play" />



        <Button

            android:id="@+id/pause"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:background="@android:drawable/ic_media_pause"

            android:onClick="pause" />



        <Button

            android:id="@+id/stop"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:background="@drawable/ic_launcher_background"

            android:onClick="stop" />

        <SeekBar

            android:id="@+id/seekBar"

            android:layout_width="match_parent"

            android:layout_height="wrap_content" />



        <TextView

            android:id="@+id/textView"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="00:00" />



    </LinearLayout>

</RelativeLayout>
 
 
 
 
 
 
 
  • 利用List View控件设计一个通讯录,要求每条信息包含头像、姓名和电话号码三部分内容

以下是几个需要修改的文件代码

程序代码:

MainActivity.java

import android.os.Bundle;

import android.widget.ListView;



import androidx.appcompat.app.AppCompatActivity;



import java.util.ArrayList;



public class MainActivity extends AppCompatActivity {



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        ArrayList<Contact> contacts = new ArrayList<>();



        contacts.add(new Contact(R.mipmap.ic_launcher, "John Doe", "123-456-7890"));

        contacts.add(new Contact(R.mipmap.monica, "Jane Doe", "098-765-4321"));



        ContactAdapter adapter = new ContactAdapter(this, contacts);



        ListView listView = (ListView) findViewById(R.id.list_view);

        listView.setAdapter(adapter);

    }

}

Contact.java

public class Contact {

    private String name;

    private String phoneNumber;

    private int avatar;



    public Contact(int avatar, String name, String phoneNumber) {

        this.avatar = avatar;

        this.name = name;

        this.phoneNumber = phoneNumber;

    }



    public String getName() {

        return name;

    }



    public String getPhoneNumber() {

        return phoneNumber;

    }



    public int getAvatar() {

        return avatar;

    }

}

ContactAdapter.java

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 java.util.ArrayList;



public class ContactAdapter extends ArrayAdapter<Contact> {

    public ContactAdapter(Context context, ArrayList<Contact> contacts) {

        super(context, 0, contacts);

    }



    @Override

    public View getView(int position, View convertView, ViewGroup parent) {

        Contact contact = getItem(position);



        if (convertView == null) {

            convertView = LayoutInflater.from(getContext()).inflate(R.layout.contact_item, parent, false);

        }



        ImageView avatarImageView = (ImageView) convertView.findViewById(R.id.avatar);

        TextView nameTextView = (TextView) convertView.findViewById(R.id.name);

        TextView phoneNumberTextView = (TextView) convertView.findViewById(R.id.phone_number);



        avatarImageView.setImageResource(contact.getAvatar());

        nameTextView.setText(contact.getName());

        phoneNumberTextView.setText(contact.getPhoneNumber());



        return convertView;

    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<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/list_view"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />



</RelativeLayout>

Contact_item.xml

<?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="wrap_content"

    android:orientation="horizontal"

    android:padding="10dp">



    <ImageView

        android:id="@+id/avatar"

        android:layout_width="50dp"

        android:layout_height="50dp"

        android:src="@mipmap/ic_launcher" />



    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="vertical"

        android:paddingLeft="10dp">



        <TextView

            android:id="@+id/name"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:textSize="18sp"

            android:textStyle="bold" />



        <TextView

            android:id="@+id/phone_number"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:textSize="16sp" />

    </LinearLayout>



</LinearLayout>

  • 利用Grid View控件设计一个展示图片的应用程序,要求以九宫格形式展示图片,并且每张照片下面要加说明文字。

以下是几个需要修改的文件代码

程序代码:

MainActivity.java

import android.os.Bundle;

import android.widget.GridView;



import androidx.appcompat.app.AppCompatActivity;



public class MainActivity extends AppCompatActivity {



    GridView gridView;



    String[] imageNames = {"Image 1", "Image 2", "Image 3", "Image 4", "Image 5", "Image 6", "Image 7", "Image 8", "Image 9"};

    int[] imageIds = {R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img1, R.drawable.img5, R.drawable.img6, R.drawable.img7, R.drawable.img8, R.drawable.img9};



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        gridView = findViewById(R.id.grid_view);



        ImageAdapter adapter = new ImageAdapter(this, imageNames, imageIds);

        gridView.setAdapter(adapter);

    }

}

ImageAdapter.java

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.TextView;



public class ImageAdapter extends BaseAdapter {

    private Context context;

    private final String[] imageNames;

    private final int[] imageIds;



    public ImageAdapter(Context context, String[] imageNames, int[] imageIds) {

        this.context = context;

        this.imageNames = imageNames;

        this.imageIds = imageIds;

    }



    @Override

    public int getCount() {

        return imageNames.length;

    }



    @Override

    public Object getItem(int position) {

        return null;

    }



    @Override

    public long getItemId(int position) {

        return 0;

    }



    @Override

    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = inflater.inflate(R.layout.grid_item, null);

        }



        TextView textView = convertView.findViewById(R.id.text_view);

        ImageView imageView = convertView.findViewById(R.id.image_view);



        textView.setText(imageNames[position]);

        imageView.setImageResource(imageIds[position]);

        return convertView;

    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent">



    <GridView

        android:id="@+id/grid_view"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:numColumns="3"

        android:verticalSpacing="10dp"

        android:horizontalSpacing="10dp"

        android:stretchMode="columnWidth"/>



</RelativeLayout>

Grid_item.xml

<?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:gravity="center_horizontal"

    android:padding="10dp">



    <ImageView

        android:id="@+id/image_view"

        android:layout_width="80dp"

        android:layout_height="80dp" />



    <TextView

        android:id="@+id/text_view"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="10dp"

        android:textSize="14sp" />

</LinearLayout>

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值