我希望用户选择是否要拍照或从图库中选择一张。
我在我的图片上设置了一个OnClickListener但是当我点击图片时,什么也没发生。
这是我的SettingUpUserProfile.java文件的代码:
public class SettingUpUserProfile extends AppCompatActivity {
public static final int TAKE_PHOTO_REQUEST = 0;
protected ImageView userProfilePicture;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting_up_user_profile);
userProfilePicture = (ImageView) findViewById(R.id.userProfilePicture);
userProfilePicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(SettingUpUserProfile.this);
builder.setTitle(null);
builder.setItems(R.array.pickImage_options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int position) {
switch (position) {
case 0:
Intent intentCaptureFromCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intentCaptureFromCamera, TAKE_PHOTO_REQUEST);
break;
case 1:
// Choose from gallery.
}
}
});
}
});
}
}
,这是我的activity_setting_up_user_profile.xml文件的代码:
<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:background="@color/light_purple"
tools:context="com.abc.xyz.SettingUpUserProfile">
<TextView
android:id="@+id/settingUpUserProfileText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="@string/settingUpUserProfileText1"
android:textColor="@color/white"
android:textStyle="bold"
android:textSize="30sp"
android:gravity="center_horizontal|center_vertical"/>
<ImageView
android:id="@+id/userProfilePicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/settingUpUserProfileText1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="100dp"
android:clickable="true"
android:src="@drawable/ic_face_white_48dp" />
<TextView
android:id="@+id/settingUpUserProfileText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/userProfilePicture"
android:layout_marginTop="5dp"
android:text="@string/settingUpUserProfileText2"
android:textColor="@color/white"
android:textSize="15sp"
android:gravity="center_horizontal|center_vertical"/>
<EditText
android:id="@+id/userName"
android:background="@drawable/phone_number_edit_text_design"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/settingUpUserProfileText2"
android:layout_marginTop="80dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:gravity="center_horizontal|center_vertical"
android:hint="@string/hint_userName"
android:textColor="@color/white"
android:textColorHint="#E0E0E0"
android:textCursorDrawable="@null"
android:inputType="textPersonName"/>
<Button
android:id="@+id/buttonAllSet"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/userName"
android:layout_marginTop="20dp"
android:text="@string/button_allSet"
android:textStyle="bold"
android:textColor="@color/light_purple"
android:layout_marginEnd="120dp"
android:layout_marginStart="120dp"
android:gravity="center_horizontal|center_vertical"/>
</RelativeLayout>
我真的无法弄清楚这里有什么问题。
请告诉我。
我是StackOverflow的新手,请合作。
提前致谢。
答案 0 :(得分:1)
将以下行添加到onClick()
方法
AlertDialog alertDialog = builder.create();
alertDialog.show();