【问题标题】:Intent not being call despite Image button being initialiized尽管已初始化图像按钮,但未调用 Intent
【发布时间】:2015-01-07 13:59:28
【问题描述】:

创建了一个图像按钮:facebook 图标和 twitter 图标。因此,当用户点击任一图标时,它会分别调用新的意图;因此,如果用户单击 facebook 图标,它将调用 facebook.class 的意图。

但是,当用户单击 facebook 图标或 twitter 图标时,它没有响应,并且在同一方法中调用了不同的意图。有人可以帮忙吗?谢谢

XML 代码:

 <!-- The navigation drawer:right drawer-->
<FrameLayout 
    android:id="@+id/preview_layout"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:background="#D8D8D8"
    android:typeface="serif" >


    <LinearLayout 
        android:id="@+id/preview_header"
        android:layout_width="match_parent"
        android:layout_height="50dp"              
        android:adjustViewBounds="true"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:textStyle="bold"
            android:text="Product Preview"
            android:textColor="#000000"
            android:typeface="serif" 
            android:textSize="20sp"/>  

 <ImageButton
     android:id= "@+id/facebook_icon"
     android:src= "@drawable/facebook"
     android:paddingRight="10dp" 
     android:paddingLeft="10dp"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:clickable="true"
     android:background="@color/transparent" 
     android:layout_toLeftOf="@+id/twitter_icon"/>
  <ImageButton 
     android:id= "@+id/twitter_icon"
     android:src= "@drawable/twitter_icon" 
     android:paddingLeft="10dp"
     android:paddingRight="10dp"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="@color/transparent" 
     android:layout_marginRight="10dp" 
     android:clickable="true"
     android:layout_alignParentRight="true"/>


    </LinearLayout>
...

代码:

listView.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

            //DAPO-DEV02:20150107: Social Media Sharing: Twitter

                ImageView TwitterBtn = (ImageView) mContex.findViewById(R.id.twitter_icon);
                TwitterBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContex.getApplicationContext(),
                                ShareTwitter.class);
                        startActivity(intent);
                    }
                });



                //DAPO-DEV02:20150107: Social Media Sharing: Facebook
                ImageButton FacebookBtn = (ImageButton) mContex.findViewById(R.id.facebook_icon);
                FacebookBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContex.getApplicationContext(),
                                ShareFacebook.class);
                        startActivity(intent);
                    }
                });

                ProductImage =(ImageView) RDrawerView.findViewById(R.id.productimage);
                String imgFilePath = url + imageLargename;
                imageLoader.displayImage(imgFilePath, ProductImage, options);


                ProductImage.setOnClickListener(new OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Toast.makeText(mContex,
                                "Pinch & Zoom To See Large Photo.",
                                Toast.LENGTH_SHORT).show();
                        String imgFilePath = url + imageLargename;
                        Intent intent = new Intent(
                                mContex.getApplicationContext(),
                                FullScreenViewActivity.class);
                        intent.putExtra("path", imgFilePath);
                        startActivity(intent);
                    }

                });

                //END OF EDITED VERSION TO DISPLAY VIEW FOR SELECTED FRAGMENT ITEM-19/11/2014
            }
        });

【问题讨论】:

  • 我认为您需要初始化图像按钮并在适配器内部而不是在 onitemclicked 中设置点击监听器
  • 为什么要在 onItemClick 中为 ImageButton 设置 setOnClickListener ?注意 onItemClick 方法代码仅在您按 ListView 行时起作用
  • @Simar 是什么意思???我不明白你
  • @ρяσѕρєяK "onItemClick" 设置为 gridView 项目,结果页面是预览片段页面,而预览片段中的图像按钮可以单击以供用户访问 facebook 或 twitter。你是什​​么意思 onItemClick 只在按下 ListView 行时才有效??

标签: android android-intent onclicklistener imagebutton


【解决方案1】:

已经设法破译问题并提出了一个可以调用次要意图的解决方案:

代码:

//DAPO-DEV02:20150107: Social Media Sharing: Twitter

final ImageButton TwitterBtn = (ImageButton) mContex.findViewById(R.id.twitter_icon);
TwitterBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        String cKey = "MNjFvsNstiMXcddTLc4J0IsHa";
                        String cSecret = "NgziPWnykalqAeMZhDUygqAYS89qJtVsehv9aQhLSlZW2myZXp";
                        final String tw="";



                        if (shareTwitter == null)
                            shareTwitter = new ShareTwitter(cKey, cSecret, ((android.app.Activity) getActivity()),
                                    getActivity().getApplicationContext(), R.drawable.twitter_icon);

                        if (socialBgAsyncTask == null) {
                            socialBgAsyncTask = (SocialBgAsyncTask) new SocialBgAsyncTask();
                            socialBgAsyncTask.execute();
                        }
                        if (twResponse == null) {
                            twResponse = "";
                            try {

                                String twitterURL = "https://twitter.com/intent/tweet?text=";
                                try {
                                    Intent i = new Intent(Intent.ACTION_VIEW);
                                    i.setData(Uri.parse(twitterURL+Uri.encode(tw)));
                                    startActivity(i);
                                } catch (android.content.ActivityNotFoundException e){
                                    Toast.makeText(getActivity(), "Can't tweet", Toast.LENGTH_SHORT).show();
                                }
                                Toast.makeText(getActivity(), "Posting Twitter data...", Toast.LENGTH_SHORT).show();
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        } else {
                            Toast.makeText(getActivity(), "Processing post data...", Toast.LENGTH_SHORT).show();
                        }
                    }
});

//DAPO-DEV02:20150108: Edited version for Social Media Sharing: facebook and twitter
private class SocialBgAsyncTask extends AsyncTask<Void, Integer, Long> {
    protected Long doInBackground(Void... params) {
        Log.i("----------SocialBgAsyncTask----------", "doInBackground");
        long result = 0;
        do {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            publishProgress(0);
        } while (!exitAsync);
        return result;
    }

    protected void onProgressUpdate(Integer... progress) {

        // ====================TWITTER
        if (twResponse != null && !twResponse.equals("")) {
            if (twResponse.equals("success"))
                Toast.makeText(getActivity(), "Message has been successfully Tweeted!", Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(getActivity(), "Failed tweeting message, please try again later.", Toast.LENGTH_SHORT).show();
            twResponse = null;
        }
    }

    protected void onPostExecute(Long result) {
        Log.i("----------SocialBgAsyncTask ----------", "onPostExecute");
    }
}

【讨论】:

    【解决方案2】:

    简单替换

    ImageView TwitterBtn = (ImageView) mContex.findViewById(R.id.twitter_icon);
    

    ImageButton TwitterBtn = (ImageButton) mContex.findViewById(R.id.twitter_icon);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 2023-01-11
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 2017-11-27
      • 1970-01-01
      相关资源
      最近更新 更多