为我添加电子邮件意图的代码后,为什么我的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);
}
}
答案 0 :(得分: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);
}
}
答案 1 :(得分:0)
实施它的方式,您只需在点击“发送”按钮后注册了按钮点击。你在send 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);
}
}