我在同一活动中有旋转器和webviewer。从微调器中选择项目时,我需要更改webviewer。我找到了代码,但是我尝试了,但webviwer没有显示任何内容,请帮助我找到此代码中的错误。
字符串:
<string-array name="Ragol">
<item value ="https://www.google.com.eg">link1</item>
<item value ="https://www.facebook.com">link2</item>
<item value ="https://www.riwaya.ml">link3</item>
<item value ="https://cooltext.com/">link4</item>
</string-array>
活动代码:
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dropDownSelector="@android:color/darker_gray"
android:dropDownWidth="@android:dimen/thumbnail_width"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="0dp" />
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="61dp">
</WebView>
Java代码
public class WebScreen extends Activity implements OnItemSelectedListener {
String starturl = "http://www.google.com.eg", selected;
Spinner s1;
WebView webView;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_screen);
String sp1 = getIntent().getStringExtra("keyName");
if (sp1.contentEquals("Ragol")) {
s1 = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence>
adapter = ArrayAdapter.createFromResource(this,
R.array.Ragol, R.layout.my_spinner);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(this);
s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
selected = parent.getItemAtPosition(pos).toString();
webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl(selected);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
webView.loadUrl(starturl);
}
});
}
}