0

为什么在我为电子邮件意图添加代码后,我的 2 个图像按钮停止工作。我一直在摆弄,Public Void onCreate bundle当我将那部分代码移到下面时,它又可以工作了。我是否错误地嵌套了它们?有人可以纠正吗?

我的 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); 
    }
}   
4

2 回答 2

0

最好不要从活动中实现接口,而是直接从对象中实现。我已经重写了你的代码,最好是这样:

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);
    }

}
于 2013-06-11T20:13:39.383 回答
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); 
    }
}   
于 2013-06-11T20:13:41.370 回答