写一个android studio 程序,java语音,布局设有按钮和图像,点击按钮可以跳转到Gridview页面选取头像后返回主界面更换头像
时间: 2024-05-02 13:22:26 浏览: 139
好的,您的问题已经记录下来了。以下是针对您的问题的答案:
您可以按照以下步骤来实现这个功能:
1. 创建一个新的Android Studio项目,并添加一个主活动(MainActivity)和一个GridView活动(GridViewActivity)。
2. 在MainActivity中添加一个按钮和一个图像视图。在XML布局文件中添加以下内容:
```
<Button
android:id="@+id/btnChangeImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Change Image" />
<ImageView
android:id="@+id/imgProfile"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/default_image" />
```
3. 在MainActivity中添加代码,以处理按钮单击事件并启动GridViewActivity:
```
public class MainActivity extends AppCompatActivity {
private static final int PICK_IMAGE = 1;
private Button btnChangeImage;
private ImageView imgProfile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnChangeImage = findViewById(R.id.btnChangeImage);
imgProfile = findViewById(R.id.imgProfile);
btnChangeImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, GridViewActivity.class);
startActivityForResult(intent, PICK_IMAGE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == RESULT_OK) {
int imageResourceId = data.getIntExtra("imageResourceId", R.drawable.default_image);
imgProfile.setImageResource(imageResourceId);
}
}
}
```
4. 在GridViewActivity中添加一个GridView,以显示头像列表。在XML布局文件中添加以下内容:
```
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:padding="10dp" />
```
5. 创建一个名为"ImageAdapter"的新类,继承自ArrayAdapter。 在该类中添加以下内容:
```
public class ImageAdapter extends ArrayAdapter<Integer> {
private Context mContext;
private int mResourceId;
private LayoutInflater mInflater;
public ImageAdapter(Context context, int resourceId, List<Integer> images) {
super(context, resourceId, images);
mContext = context;
mResourceId = resourceId;
mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(mResourceId, parent, false);
}
ImageView imageView = convertView.findViewById(R.id.imgItem);
imageView.setImageResource(getItem(position));
return convertView;
}
}
```
6. 在GridViewActivity中添加代码,以载入并显示头像列表:
```
public class GridViewActivity extends AppCompatActivity {
private GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gridview);
gridView = findViewById(R.id.gridView);
List<Integer> images = new ArrayList<>();
images.add(R.drawable.animal1);
images.add(R.drawable.animal2);
images.add(R.drawable.animal3);
images.add(R.drawable.animal4);
images.add(R.drawable.animal5);
images.add(R.drawable.animal6);
images.add(R.drawable.animal7);
images.add(R.drawable.animal8);
images.add(R.drawable.animal9);
ImageAdapter adapter = new ImageAdapter(this, R.layout.item_image, images);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int imageResourceId = (int) parent.getItemAtPosition(position);
Intent intent = new Intent();
intent.putExtra("imageResourceId", imageResourceId);
setResult(RESULT_OK, intent);
finish();
}
});
}
}
```
7. 最后,在res/drawable文件夹中添加头像的图片,并将它们命名为"animal1.png","animal2.png",等等。
这样,您现在已经可以编写一个能够在单击按钮时打开GridViewActivity并在选择新头像后返回MainActivity的Android Studio程序。希望这能够对您有所帮助!
阅读全文