Noob在这里。我正在学习创建一个简单的Android网络浏览器。我遇到的问题是我有一个ListView
有不同的网站列表,Activity
有WebView
。但是,我只能从此活动启动一个URL。我希望在点击ListView
中的其他项目时将网址替换为其他网站。
我不想为每个链接创建新的Activity
。这是我的代码。
MainActivity.java
package firstapp.listviewtutorial;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
String url = getIntent().getStringExtra("url");
final String TAG = this.getClass().getName();
ArrayAdapter<String> adapter;
ListView listView;
String[] website_names = {"Google Search","Wikipedia Search", "Youtube Search","World News",
"Facebook", "Twitter", "Amazon Shopping","About Us","Exit"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.menu_item);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, website_names);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
Intent gs = new Intent(MainActivity.this, WebViewActivity.class);
startActivity(gs);
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
Intent about = new Intent(MainActivity.this, AboutDev.class);
startActivity(about);
break;
case 8:
System.exit(0);
break;
}
}
});
}
boolean twice=false;
@Override
public void onBackPressed() {
if(twice == true){
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
System.exit(0);
}
twice = true;
Log.d(TAG, "Twice: " +twice);
// super.onBackPressed();
Toast.makeText(MainActivity.this, "Press Again to Exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
twice = false;
Log.d(TAG, "Twice: " +twice);
}
},3000);
}
}
WebViewActivity.java
package firstapp.listviewtutorial;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import static firstapp.listviewtutorial.R.id.webView;
public class WebViewActivity extends AppCompatActivity {
WebView wb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google_search);
wb = (WebView) findViewById(webView);
wb.setWebViewClient(new WebViewClient());
WebSettings webSettings = wb.getSettings();
webSettings.setJavaScriptEnabled(true);
wb.loadUrl("http://www.google.com");
}
@Override
public void onBackPressed() {
if (wb.canGoBack()) {
wb.goBack();
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.item_option1:
wb.reload();
break;
case R.id.item_option2:
Intent home = new Intent(WebViewActivity.this, MainActivity.class);
startActivity(home);
break;
case R.id.item_option3:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
break;
case R.id.item_option4:
Intent about = new Intent(WebViewActivity.this, AboutDev.class);
startActivity(about);
break;
case R.id.item_option5:
System.exit(0);
break;
}
return super.onOptionsItemSelected(item);
}
}
我还需要包含其他内容吗?谢谢大家。
答案 0 :(得分:2)
在你做的这部分
case 0:
Intent gs = new Intent(MainActivity.this, WebViewActivity.class);
//ADD THIS LINE
gs.putExtra("URL", "<whatever_url_for_case_0>");
startActivity(gs);
对所有列表项和网址执行相同的操作。
WebViewActivity.java中的
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google_search);
wb = (WebView) findViewById(webView);
wb.setWebViewClient(new WebViewClient());
WebSettings webSettings = wb.getSettings();
webSettings.setJavaScriptEnabled(true);
//Retrieve the url that you put into your intent
String url = getIntent().getStringExtra("URL");
wb.loadUrl(url);
}