2016-12-01 87 views
0

我的佈局很好,只剩下幾個問題:我無法使用mailto和facebook鏈接按鈕。這是我已經試過:Android上的按鈕問題

ImageView Button = (ImageView)findViewById(R.id.yourButtonsId); 

Button.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
     Intent intent = new Intent(); 
     intent.setAction(Intent.ACTION_VIEW); 
     intent.addCategory(Intent.CATEGORY_BROWSABLE); 
     intent.setData(Uri.parse("http://www.yourURL.com")); 
     startActivity(intent); 
    } 
}); 

TextView textView = (TextView)findViewById(R.id.yourID); 

textView.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
     Intent intent = new Intent(); 
     intent.setAction(Intent.ACTION_VIEW); 
     intent.addCategory(Intent.CATEGORY_BROWSABLE); 
     intent.setData(Uri.parse("http://www.typeyourURL.com")); 
     startActivity(intent); 
    } }); 

回答

0

如果你想使用的mailto不是把一個EditText在XML和郵件的按鈕。 用戶將在edittext中添加他的電子郵件ID,然後按按鈕發送郵件。當發送按鈕被點擊時,你可以寫如

Button.setOnClickListener(new OnClickListener() { 
public void onClick(View v) { 

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
    "mailto",yourEditTextObj.getText(), null)); 
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject"); 
emailIntent.putExtra(Intent.EXTRA_TEXT, "Body"); 
startActivity(Intent.createChooser(emailIntent, "Send email...")); 
} 
}); 
0
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
     "mailto","[email protected]", null)); 
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject"); 
emailIntent.putExtra(Intent.EXTRA_TEXT, "Body"); 
startActivity(Intent.createChooser(emailIntent, "Send email...")); 
+0

嗨,感謝您的迴應,但我該如何使用此代碼?我必須爲此做一個xml嗎? im在mainactivity.xml我不知道如何解決android – err0r

+0

把它放到onClick() – ZeroOne