【发布时间】:2016-09-11 11:39:31
【问题描述】:
这是主要活动
<?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="com.example.dayyani.finalproject.MainActivity">
<ImageButton
android:id="@+id/imageSelection"
android:layout_centerHorizontal="true"
android:layout_marginTop="10sp"
android:layout_width="80sp"
android:layout_height="80sp"
android:background="@drawable/profile"
android:onClick="choseProfile"/>
<EditText
android:id="@+id/firestEdit"
android:layout_below="@+id/imageSelection"
android:layout_centerHorizontal="true"
android:layout_width="200sp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"/>
<EditText
android:id="@+id/secondtEdit"
android:layout_below="@+id/firestEdit"
android:layout_centerHorizontal="true"
android:layout_width="200sp"
android:layout_height="wrap_content"/>
package com.example.dayyani.finalproject;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton imageSelection = (ImageButton) findViewById(R.id.imageSelection);
EditText firstEdit = (EditText) findViewById(R.id.firestEdit);
EditText secondtEdit = (EditText) findViewById(R.id.secondtEdit);
}
public void choseProfile(View v) {
LayoutInflater inflater = this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.dialog_selection, null);
final AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("chose profile")
.setView(dialogView)
.setNegativeButton("back", null)
.show();
}
public void changeImageBackground(View v) {
View.OnClickListener change = new View.OnClickListener() {
@Override
public void onClick(View view) {
final ImageButton imageSelection = (ImageButton) findViewById(R.id.imageSelection);
final ImageButton firstImage = (ImageButton) findViewById(R.id.firstImage);
ImageButton secondImage = (ImageButton) findViewById(R.id.secondImage);
ImageButton thirdImage = (ImageButton) findViewById(R.id.thirdImage);
ImageButton fourthImage = (ImageButton) findViewById(R.id.fourthImage);
firstImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imageSelection.setBackground(getDrawable(R.drawable.profile));
}
});
secondImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imageSelection.setBackground(getDrawable(R.drawable.profile2));
}
});
}
};
}
}
这是对话框
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="@+id/firstImage"
android:layout_width="80sp"
android:layout_height="80sp"
android:background="@drawable/profile"
android:onClick="changeImageBackground"
android:clickable="true"/>
<TextView
android:id="@+id/firstText"
android:layout_toRightOf="@+id/firstImage"
android:layout_width="250sp"
android:layout_height="80sp"
android:text="firstImage"/>
<ImageButton
android:id="@+id/secondImage"
android:layout_below="@+id/firstImage"
android:layout_width="80sp"
android:layout_height="80sp"
android:background="@drawable/profile2"
android:onClick="changeImageBackground"
android:clickable="true"/>
<TextView
android:id="@+id/secondText"
android:layout_toRightOf="@+id/secondImage"
android:layout_below="@+id/firstText"
android:layout_width="250sp"
android:layout_height="80sp"
android:text="secondImage"/>
<ImageButton
android:id="@+id/thirdImage"
android:layout_below="@+id/secondImage"
android:layout_width="80sp"
android:layout_height="80sp"
android:clickable="true"/>
<TextView
android:id="@+id/thirdText"
android:layout_toRightOf="@+id/thirdImage"
android:layout_below="@+id/secondText"
android:layout_width="250sp"
android:layout_height="80sp"
android:text="thirdImage"/>
<ImageButton
android:id="@+id/fourthImage"
android:layout_below="@+id/thirdImage"
android:layout_width="80sp"
android:layout_height="80sp"
android:clickable="true"/>
<TextView
android:id="@+id/fourthText"
android:layout_toRightOf="@+id/fourthImage"
android:layout_below="@+id/thirdText"
android:layout_width="250sp"
android:layout_height="80sp"
android:text="thirdImage"/>
</RelativeLayout>
我想要的是,如果我单击对话框上的图像,他将使用此图像背景并将其放在活动图像上
例如,当我在对话框中单击 firstImage 时,imageSelection 背景更改为 firstImage 背景
感谢您的帮助!!!!
【问题讨论】:
标签: android background dialog android-alertdialog imagebutton