2013-06-11 115 views
0

爲我的電子郵件意圖添加代碼後,爲什麼我的2圖像按鈕停止工作。我一直在擺弄Public Void onCreate bundle,當我移動下面的代碼段時,它再次起作用。我把它們錯誤地嵌套了嗎?有人可以糾正它嗎?爲什麼我的2個按鈕停止工作?

我的OnClickListener基本上不工作,因爲按鈕單擊時什麼也不做。

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.webkit.WebView; 
import android.widget.Button; 
import android.widget.ImageButton; 

public class contactActivity extends Activity implements OnClickListener { 

    private WebView webView; 
    private WebView webView1; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.contact); 
     Button mail = (Button)findViewById(R.id.Sendbutton); 
     mail.setOnClickListener(this); 
    } 

    public void onClick(View v) { 
     switch(v.getId()) 
     { 
      case R.id.Sendbutton: 
       Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
       String[] recipients = new String[]{"email add", "",}; 
       emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients); 
       emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Sample mail"); 
       emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is a sample mail.."); 
       emailIntent.setType("text/plain"); 
       startActivity(Intent.createChooser(emailIntent, "Send mail client :")); 
       finish(); 
       break; 
     } 

     ImageButton ViewFacebookFeed = (ImageButton) findViewById(R.id.ViewFacebookFeed); 
     ImageButton ViewTwitterFeed = (ImageButton) findViewById (R.id.ViewTwitterFeed); 

     //Add a listener to ImageButton1 
     ViewFacebookFeed.setOnClickListener(new OnClickListener() {      
      @Override 
      public void onClick(View v) { 
       openBrowser1(); 
      }   
     });        
     ViewTwitterFeed.setOnClickListener(new OnClickListener() {      
      @Override 
      public void onClick(View v) { 
       openBrowser2(); 
      }   
     }); 
    } 
    private void openBrowser1() 
    {  
     //this intent is used to open other activity which contains another webView 
     Intent intent = new Intent(contactActivity.this,fbtwitterfeedActivity.class); 
     startActivity(intent); 
    } 
    private void openBrowser2() { 
     // TODO Auto-generated method stub 
     Intent intent1 = new Intent(contactActivity.this,twitterfeedActivity.class); 
     startActivity(intent1); 
    } 
} 
+0

你怎麼知道「onClickListeners不工作」處理所有的點擊? – Simon

+0

你的OnClickListener被調用了嗎?你可以在那裏寫一條日誌語句來驗證? – CaseyB

+0

@CaseyB爲什麼要添加代碼?爲什麼不只是一個斷點? – Simon

回答

0

您把它實現的方式,你只註冊了按鈕點擊,你點擊「發送」按鈕後。您在發送onClick時調用finish(),這樣偵聽器將永遠不會被註冊。在onCreate中註冊監聽器,然後在onClick方法中監聽特定視圖ID的點擊。讓你在適當的時候註冊的點擊監聽器,我已經修復你的代碼,你的onClick方法從所有按鈕

public class contactActivity extends Activity implements OnClickListener { 

    private WebView webView; 
    private WebView webView1; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.contact); 
     Button mail = (Button)findViewById(R.id.Sendbutton); 
     mail.setOnClickListener(this); 

     ImageButton viewFacebookFeed = (ImageButton) findViewById(R.id.ViewFacebookFeed); 
     viewFacebookFeed.setOnClickListener(this); 
     ImageButton viewTwitterFeed = (ImageButton) findViewById (R.id.ViewTwitterFeed); 
     viewTwitterFeed.setOnClickListener(this); 
    } 

    public void onClick(View v) { 
     switch(v.getId()) 
     { 
      case R.id.Sendbutton: 
       Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
       String[] recipients = new String[]{"email add", "",}; 
       emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients); 
       emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Sample mail"); 
       emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is a sample mail.."); 
       emailIntent.setType("text/plain"); 
       startActivity(Intent.createChooser(emailIntent, "Send mail client :")); 
       finish(); 
       break; 
      case R.id.ViewFacebookFeed: 
       openBrowser1(); 
       break; 
      case R.id.ViewTwitterFeed: 
       openBrowser2(); 
       break; 

     } 
    } 
    private void openBrowser1() 
    {  
     //this intent is used to open other activity which contains another webView 
     Intent intent = new Intent(contactActivity.this,fbtwitterfeedActivity.class); 
     startActivity(intent); 
    } 
    private void openBrowser2() { 
     // TODO Auto-generated method stub 
     Intent intent1 = new Intent(contactActivity.this,twitterfeedActivity.class); 
     startActivity(intent1); 
    } 
} 
+0

非常感謝這麼多代碼的作品。你知道如何添加輸入字段和複選框來發送電子郵件 –

+0

你需要添加CheckBox和EditText元素到你的XML佈局文件,然後在代碼中引用它們。 – dymmeh

+0

我有他們在我的XML,但無法編碼,以便他們在電子郵件中。我的發送'詳細信息按鈕'起作用,因爲它彈出電子郵件選擇器的彈出式菜單,但在消息中,我無​​法獲取姓名電話日期。我的輸入字段的ID是R.id.Nameinput,R.id.Phoneinput,R.id.dateinput ..... Checkboxes是R.id.Checkbox1 .Checkbox2 .Checkbox3。請指教 –

0

最好不要從活動中實現接口,而要從對象直接實現接口。 我已經rewritted你的代碼,它是這樣的好:

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.webkit.WebView; 
import android.widget.Button; 
import android.widget.ImageButton; 

public class contactActivity extends Activity { 

    private WebView webView; 
    private WebView webView1; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.contact); 
     Button mail = (Button) findViewById(R.id.Sendbutton); 
     ImageButton ViewFacebookFeed = (ImageButton) findViewById(R.id.ViewFacebookFeed); 
     ImageButton ViewTwitterFeed = (ImageButton) findViewById(R.id.ViewTwitterFeed); 
     // Add a listener to ImageButton1 
     ViewFacebookFeed.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       openBrowser1(); 
      } 
     }); 
     ViewTwitterFeed.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       openBrowser2(); 
      } 
     }); 
     mail.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
       String[] recipients = new String[] { "email add", "", }; 
       emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients); 
       emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Sample mail"); 
       emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is a sample mail.."); 
       emailIntent.setType("text/plain"); 
       startActivity(Intent.createChooser(emailIntent, "Send mail client :")); 
       finish(); 
      } 

     }); 
    } 

    private void openBrowser1() { 
     // this intent is used to open other activity which contains another 
     // webView 
     Intent intent = new Intent(contactActivity.this, fbtwitterfeedActivity.class); 
     startActivity(intent); 
    } 

    private void openBrowser2() { 
     // TODO Auto-generated method stub 
     Intent intent1 = new Intent(contactActivity.this, twitterfeedActivity.class); 
     startActivity(intent1); 
    } 

} 
相關問題