2013-08-04 41 views
1

我正在創建一個簡單的主 - 細節應用程序,我希望用戶能夠雙擊細節視圖屏幕,並從默認的電子郵件應用程序對話框打開設備。以下附件是我的TopicDetalsFrament.java文件。請幫我理解爲什麼OnDoubleTapListener代碼不起作用。試圖通過雙擊片段啓動電子郵件應用程序

public class TopicDetailFragment extends Fragment implements OnDoubleTapListener 
{ 
/** 
* The fragment argument representing the item ID that this fragment 
* represents. 
*/ 
public static final String ARG_ITEM_ID = "item_id"; 

/** 
* The topic content this fragment is presenting. 
*/ 
private TopicArrayContent.TopicArrayItem mItem; 

/** 
* Mandatory empty constructor for the fragment manager to instantiate the 
* fragment (e.g. upon screen orientation changes). 
*/ 
public TopicDetailFragment() 
{ 

} 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    if (getArguments().containsKey(ARG_ITEM_ID)) 
    { 
     // Load the content specified by the fragment arguments. 
     mItem = TopicArrayContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID)); 
    } 

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) 
{ 
    View rootView = inflater.inflate(R.layout.fragment_topic_detail, container, false); 

    // Show the Scriptures content as text in a TextView. 
    if (mItem != null) 
    {   
     ((TextView) rootView.findViewById(R.id.topic_scripture1)).setText(mItem.scripture1); 
     ((TextView) rootView.findViewById(R.id.topic_scripture2)).setText(mItem.scripture2); 
     ((TextView) rootView.findViewById(R.id.topic_scripture3)).setText(mItem.scripture3); 
     ((TextView) rootView.findViewById(R.id.topic_scripture4)).setText(mItem.scripture4); 
     ((TextView) rootView.findViewById(R.id.topic_scripture5)).setText(mItem.scripture5); 
     ((TextView) rootView.findViewById(R.id.topic_scripture6)).setText(mItem.scripture6); 
     ((TextView) rootView.findViewById(R.id.topic_scripture7)).setText(mItem.scripture7); 
     ((TextView) rootView.findViewById(R.id.topic_name)).setText(mItem.topicName); 
    } 
    return rootView;   

} 

@Override 
public boolean onDoubleTap(MotionEvent e) { 
    // TODO Auto-generated method stub 
    Intent i = new Intent(Intent.ACTION_SEND); 
    i.setType("message/rfc822"); 
    i.putExtra(Intent.EXTRA_EMAIL, 
      new String[] { "Enter recipient's email address" }); 
    i.putExtra(Intent.EXTRA_SUBJECT, "subject of email"); 
    i.putExtra(Intent.EXTRA_TEXT, "body of email"); 
    try { 
     startActivity(Intent.createChooser(i, "Send mail...")); 
    } catch (android.content.ActivityNotFoundException ex) { 
     Toast.makeText(TopicDetailFragment.this.getActivity(), 
       "There are no email clients installed.",   Toast.LENGTH_LONG) 
       .show(); 
    } 
    return true; 
} 

@Override 
public boolean onDoubleTapEvent(MotionEvent e) { 
    // TODO Auto-generated method stub  
    return true; 
} 

@Override 
public boolean onSingleTapConfirmed(MotionEvent e) { 
    // TODO Auto-generated method stub 
    return false; 
} 

} 

非常感謝任何幫助/建議,你可以給,

SonCoder

+0

我發現很多關於如何在活動上做到這一點的信息,但沒有太多關於在片段上實現的信息。然而,我是一個初學者,所以還有很多東西需要學習。有人有主意嗎?有更好的方法可以指出你可以使用的例子/教程嗎? – dmiannay

回答

2

好了,你根本就沒有註冊任何事件。實現界面並不神奇地爲您提供輕拍事件。您必須將該片段安裝爲偵聽器。

請參閱gesture detection training並設置手勢檢測器,並將片段作爲onCreate方法中的回調。


除此之外:恕我直言,這種模式在Android上不是很好的做法。用戶不會知道他必須雙擊屏幕才能執行操作。爲什麼不提供可點擊的電子郵件地址(請參閱默認的Android人員應用程序)或操作欄菜單條目?

+0

欣賞關於使用可點擊的電子郵件地址的提示。我會將其視爲首選的替代方案。也感謝鏈接到「手勢檢測訓練」。 – dmiannay

+0

很高興能夠幫助:) – Patrick

相關問題