我正在尝试添加一个通话按钮。这将是button14。所有其他按钮都会打开一个 WebActivity 文件,其中包含字符串作为链接。
我对 Android 编程很陌生。我首先有人帮我创建了应用程序,但他不在城里,我需要完成这项工作。任何帮助都会很棒!
package com.myapp.programname;
import com.myapp.programname.R;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onButton(View view) {
int index = -1;
int id = view.getId();
if (id == R.id.button1) {
index = 0;
}
else if (id == R.id.button2) {
index = 1;
}
else if (id == R.id.button3) {
index = 2;
}
else if (id == R.id.button4) {
index = 3;
}
else if (id == R.id.button5) {
index = 4;
}
else if (id == R.id.button6) {
index = 5;
}
else if (id == R.id.button7) {
index = 6;
}
else if (id == R.id.button8) {
index = 7;
}
else if (id == R.id.button9) {
index = 8;
}
else if (id == R.id.button10) {
index = 9;
}
else if (id == R.id.button11) {
index = 10;
}
else if (id == R.id.button12) {
index = 11;
}
else if (id == R.id.button13) {
index = 12;
}
else if (id == R.id.button14) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:123456789"));
startActivity(callIntent);
}
else {
index = 0;
}
Intent intent = new Intent(this, WebActivity.class);
intent.putExtra("link", index);
this.startActivity(intent);
}
}
这里也是 WebActivity 代码
package com.myapp.programname;
import com.myapp.programname.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebActivity extends Activity {
private class MyWebViewClient extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
}
String links[] = {"http://somesite.com",
"http://somesite.com",
"http://somesite.com",
"http://somesite.com",
"http://somesite.com",
"http://somesite.com",
"http://somesite.com",
"http://somesite.com",
"http://somesite.com,
"http://somesite.com",
"http://somesite.com",
"http://somesite.com",
"http://somesite.com"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
int index = getIntent().getIntExtra("link", 0);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setLoadWithOverviewMode(true);
myWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.loadUrl(links[index]);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.web, menu);
return true;
}
}