不会在弹出菜单项单击和替代活动上启动的活动打开

时间:2015-03-26 11:24:59

标签: android xml android-intent

我所拥有的是弹出菜单,其中存在两个活动。一个用于编辑,第二个用于删除。发生的事情就是当我点击编辑时没有任何反应,但是当我点击删除时它会带我去编辑活动。我在logcat中什么也没得到。

token.xml:

 <menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/action_edit"
    android:icon="@drawable/ic_action_edit"
    android:title="@string/edit"/>
<item
    android:id="@+id/action_delete"
    android:icon="@android:drawable/ic_menu_delete"
    android:title="@string/delete" />
</menu>

TokenAdapter.java

     protected void bindView(View view, final int position) {
    final Context ctx = view.getContext();
    TokenLayout tl = (TokenLayout) view;
    Token token = getItem(position);

    tl.bind(token, org.fedorahosted.freeotp.R.menu.token, new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent i;

            switch (item.getItemId()) {
                case org.fedorahosted.freeotp.R.id.action_edit:

                    i = new Intent(ctx, EditActivity.class);
                    i.putExtra(EditActivity.EXTRA_POSITION, position);
                    ctx.startActivity(i);
                    return true;


                case org.fedorahosted.freeotp.R.id.action_delete:

                    Intent i2 = new Intent(ctx, DeleteActivity.class);
                    i2.putExtra(DeleteActivity.EXTRA_POSITION, position);
                    ctx.startActivity(i2);
                    return true;

                default:
                    return false;

            }


        }
    });

    tl.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TokenPersistence tp = new TokenPersistence(ctx);

            // Increment the token.
            Token token = tp.get(position);
            TokenCode codes = token.generateCodes();
            tp.save(token);

            // Copy code to clipboard.
            mClipMan.setPrimaryClip(ClipData.newPlainText(null, codes.getCurrentCode()));
            Toast.makeText(v.getContext().getApplicationContext(),
                    org.fedorahosted.freeotp.R.string.code_copied,
                    Toast.LENGTH_SHORT).show();

            mTokenCodes.put(token.getID(), codes);
            ((TokenLayout) v).start(token.getType(), codes, true);
        }
    });

    TokenCode tc = mTokenCodes.get(token.getID());
    if (tc != null && tc.getCurrentCode() != null)
        tl.start(token.getType(), tc, false);
}

的AndroidManifest.xml

    <activity
        android:name="org.fedorahosted.freeotp.edit.DeleteActivity"
        android:label="@string/delete_question"
        android:theme="@android:style/Theme.Holo.Light.Dialog"/>

    <activity
        android:name="org.fedorahosted.freeotp.edit.EditActivity"
        android:theme="@android:style/Theme.Holo.Light.Dialog.NoActionBar"
        android:windowSoftInputMode="stateVisible" />

TokenLayout.java

package org.fedorahosted.freeotp;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

public class TokenLayout extends FrameLayout implements   View.OnClickListener, Runnable {
private ProgressCircle mProgressInner;
private ProgressCircle mProgressOuter;
private ImageView mImage;
private TextView mCode;
private TextView mIssuer;
private TextView mLabel;
private ImageView mMenu;
private PopupMenu mPopupMenu;

private TokenCode mCodes;
private Token.TokenType mType;
private String mPlaceholder;
private long mStartTime;

public TokenLayout(Context context) {
    super(context);
}

public TokenLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public TokenLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onFinishInflate() {
    super.onFinishInflate();

    mProgressInner = (ProgressCircle)   findViewById(org.fedorahosted.freeotp.R.id.progressInner);
    mProgressOuter = (ProgressCircle) findViewById(org.fedorahosted.freeotp.R.id.progressOuter);
    mImage = (ImageView) findViewById(org.fedorahosted.freeotp.R.id.image);
    mCode = (TextView) findViewById(org.fedorahosted.freeotp.R.id.code);
    mIssuer = (TextView) findViewById(org.fedorahosted.freeotp.R.id.issuer);
    mLabel = (TextView) findViewById(org.fedorahosted.freeotp.R.id.label);
    mMenu = (ImageView) findViewById(org.fedorahosted.freeotp.R.id.menu);

    mPopupMenu = new PopupMenu(getContext(), mMenu);
    mMenu.setOnClickListener(this);
}

 public void bind(Token token, int menu, PopupMenu.OnMenuItemClickListener  micl) {
    mCodes = null;

    // Setup menu.
    mPopupMenu.getMenu().clear();
    mPopupMenu.getMenuInflater().inflate(menu, mPopupMenu.getMenu());
    mPopupMenu.setOnMenuItemClickListener(micl);

    // Cancel all active animations.
    setEnabled(true);
    removeCallbacks(this);
    mImage.clearAnimation();
    mProgressInner.clearAnimation();
    mProgressOuter.clearAnimation();
    mProgressInner.setVisibility(View.GONE);
    mProgressOuter.setVisibility(View.GONE);

    // Get the code placeholder.
    char[] placeholder = new char[token.getDigits()];
    for (int i = 0; i < placeholder.length; i++)
        placeholder[i] = '-';
    mPlaceholder = new String(placeholder);

    // Show the image.
    Picasso.with(getContext())
            .load(token.getImage())
            .placeholder(org.fedorahosted.freeotp.R.drawable.logo)
            .into(mImage);

    // Set the labels.
    mLabel.setText(token.getLabel());
    mIssuer.setText(token.getIssuer());
    mCode.setText(mPlaceholder);
    if (mIssuer.getText().length() == 0) {
        mIssuer.setText(token.getLabel());
        mLabel.setVisibility(View.GONE);
    } else {
        mLabel.setVisibility(View.VISIBLE);
    }
 }

 private void animate(View view, int anim, boolean animate) {
    Animation a = AnimationUtils.loadAnimation(view.getContext(), anim);
    if (!animate)
        a.setDuration(0);
    view.startAnimation(a);
 }

 public void start(Token.TokenType type, TokenCode codes, boolean animate) {
    mCodes = codes;
    mType = type;

    // Start animations.
    mProgressInner.setVisibility(View.VISIBLE);
    animate(mProgressInner, org.fedorahosted.freeotp.R.anim.fadein,  animate);
    animate(mImage, org.fedorahosted.freeotp.R.anim.token_image_fadeout,  animate);

    // Handle type-specific UI.
    switch (type) {
        case HOTP:
            setEnabled(false);
            break;
        case TOTP:
            mProgressOuter.setVisibility(View.VISIBLE);
            animate(mProgressOuter, org.fedorahosted.freeotp.R.anim.fadein,  animate);
            break;
    }

    mStartTime = System.currentTimeMillis();
    post(this);
}

@Override
public void onClick(View v) {
    mPopupMenu.show();
}

@Override
public void run() {
    // Get the current data
    String code = mCodes == null ? null : mCodes.getCurrentCode();
    if (code != null) {
        // Determine whether to enable/disable the view.
        if (!isEnabled())
            setEnabled(System.currentTimeMillis() - mStartTime > 5000);

        // Update the fields
        mCode.setText(code);
        mProgressInner.setProgress(mCodes.getCurrentProgress());
        if (mType != Token.TokenType.HOTP)
            mProgressOuter.setProgress(mCodes.getTotalProgress());

        postDelayed(this, 100);
        return;
    }

    mCode.setText(mPlaceholder);
    mProgressInner.setVisibility(View.GONE);
    mProgressOuter.setVisibility(View.GONE);
    animate(mImage, org.fedorahosted.freeotp.R.anim.token_image_fadein,  true);
  }
}

1 个答案:

答案 0 :(得分:-1)

试试这个..

tl.bind(token, org.fedorahosted.freeotp.R.menu.token, new   PopupMenu.OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem item) {

        switch (item.getItemId()) {
            case org.fedorahosted.freeotp.R.id.action_edit:

                Intent i = new Intent(ctx, EditActivity.class);
                i.putExtra(EditActivity.EXTRA_POSITION, position);
                ctx.startActivity(i);
                return true;

            case org.fedorahosted.freeotp.R.id.action_delete:

                Intent i2 = new Intent(ctx, DeleteActivity.class);
                i2.putExtra(DeleteActivity.EXTRA_POSITION, position);
                ctx.startActivity(i2);
                return true;

            default:
                return false;

        }

        return false;
    }
});