比较drawable图像与imageview图标android?

时间:2018-01-11 11:38:44

标签: android android-layout android-drawable

我将imageview与drawable图像进行比较。如果它是相同的,那么我需要写一个逻辑。

如果不相同,我需要处理另一个逻辑。

所以我尝试的是,

private ImageView emailIcon, phoneIcon;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contentView = inflater.inflate(R.layout.activity_summary_activities, null);{
 emailIcon = (ImageView) findViewById(R.id.email_icon);
        phoneIcon = (ImageView) findViewById(R.id.sms_icon);

 phoneIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                phoneIcon.setBackgroundResource(R.drawable.round);
                emailIcon.setBackgroundResource(R.drawable.ic_mark_white);
            }

        });

        emailIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                phoneIcon.setBackgroundResource(R.drawable.ic_mark_white);
                emailIcon.setBackgroundResource(R.drawable.round);
            }

        });

    submitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (phoneIcon.getDrawable().getConstantState() == SummaryActivities.this
                        .getResources().getDrawable(R.drawable.ic_mark_white)
                        .getConstantState()) {
                    Log.e("going here", "going here");
                }
                }

但我正在崩溃,

java.lang.NullPointerException:尝试调用虚方法' android.graphics.drawable.Drawable $ ConstantState android.graphics.drawable.Drawable.getConstantState()'在null对象引用上                                                                             在com.view.SummaryActivities $ 5.onClick(SummaryActivities.java:130)

xml中的

   <ImageView
                        android:id="@+id/sms_icon"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_alignParentLeft="true"
                        android:layout_marginLeft="32dp"
                        android:layout_marginTop="3dp"
                        android:background="@drawable/ic_mark_white" />
 <ImageView
                        android:id="@+id/email_icon"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_alignParentLeft="true"
                        android:layout_marginLeft="32dp"
                        android:layout_marginTop="2dp"
                        android:background="@drawable/ic_mark_white" />

2 个答案:

答案 0 :(得分:1)

使用setBackgroundResource

的setImageResource
 private ImageView emailIcon, phoneIcon;
   @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    contentView = inflater.inflate(R.layout.activity_summary_activities, 
   null);{
  emailIcon = (ImageView) findViewById(R.id.email_icon);
    phoneIcon = (ImageView) findViewById(R.id.sms_icon);

  phoneIcon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            phoneIcon.setImageResource(R.drawable.round);
            emailIcon.setImageResource(R.drawable.ic_mark_white);
        }

    });

    emailIcon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            phoneIcon.setImageResource(R.drawable.ic_mark_white);
            emailIcon.setImageResource(R.drawable.round);
        }

    });

  submitBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (phoneIcon.getDrawable().getConstantState() == 
   SummaryActivities.this
                    .getResources().getDrawable(R.drawable.ic_mark_white)
                    .getConstantState()) {
                Log.e("going here", "going here");
            }
      }

答案 1 :(得分:0)

添加contentView并在onCreate()中初始化submitBtn,如下所示

private ImageView emailIcon, phoneIcon;
private submitBtn; //change here
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contentView = inflater.inflate(R.layout.activity_summary_activities, null);{
 emailIcon = (ImageView) contentView.findViewById(R.id.email_icon);
        phoneIcon = (ImageView) contentView.findViewById(R.id.sms_icon);   
        submitBtn = (Button)contentView.findViewById(R.id.button_id);  //change here

 phoneIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                phoneIcon.setBackgroundResource(R.drawable.round);
                emailIcon.setBackgroundResource(R.drawable.ic_mark_white);
            }

        });

        emailIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                phoneIcon.setBackgroundResource(R.drawable.ic_mark_white);
                emailIcon.setBackgroundResource(R.drawable.round);
            }

        });

    submitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (phoneIcon.getDrawable().getConstantState() == SummaryActivities.this
                        .getResources().getDrawable(R.drawable.ic_mark_white)
                        .getConstantState()) {
                    Log.e("going here", "going here");
                }
                }