標題:
AlertDialog 放入webView瀏覽網頁
[打印本頁]
作者:
周政輝
時間:
2018-11-3 11:36
標題:
AlertDialog 放入webView瀏覽網頁
利用 動態建立的 WebView 放入AlertDialog作顯示
作者:
蔡季樺
時間:
2018-11-4 14:14
package com.example.plantsai.myapplicationpic;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button p;
Button n;
ImageView iv;
int num = 1;
TextView tv;
int[] image = new int[6];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init()
{
n = findViewById(R.id.button2);
p = findViewById(R.id.button);
iv = findViewById(R.id.imageView);
tv = findViewById(R.id.textView);
image[0] = R.drawable.img01;
image[1] = R.drawable.img02;
image[2] = R.drawable.img03;
image[3] = R.drawable.img04;
image[4] = R.drawable.img05;
image[5] = R.drawable.img06;
iv.setImageResource(image[0]);
rename();
n.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(num != 6)
{
num++;
rename();
iv.setImageResource(image[num-1]);
}else{
rename();
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
final View view = inflater.inflate(R.layout.testlayout, null);
ImageView imageView = view.findViewById(R.id.imageView2);
imageView.setImageResource(R.drawable.ic_launcher_foreground);
WebView wv = new WebView(MainActivity.this);
wv.loadUrl("http://www.google.com");
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
new AlertDialog.Builder(MainActivity.this)
.setTitle("錯誤訊息")
.setMessage("已經到底了!")
.setView(view)
.setView(wv)
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}
}
});
p.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(num != 1)
{
num--;
rename();
iv.setImageResource(image[num-1]);
}else{
rename();
// LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
// final View view = inflater.inflate(R.layout.testlayout, null);
// ImageView imageView = view.findViewById(R.id.imageView2);
// imageView.setImageResource(R.drawable.ic_launcher_foreground);
WebView wv = new WebView(MainActivity.this);
wv.loadUrl("http://www.google.com");
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
new AlertDialog.Builder(MainActivity.this)
.setTitle("錯誤訊息")
.setMessage("已經到底了!")
.setCancelable(false)
.setView(wv)
// .setView(view)
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}
}
});
}
public void rename()
{
tv.setText("第"+num+"/6張");
}
}
複製代碼
<?xml version="1.0" encoding="utf-8"?>
<android.widget.AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="289dp"
android:layout_height="219dp"
android:layout_x="50dp"
android:layout_y="136dp"
app:srcCompat="@drawable/img01"
tools:layout_editor_absoluteX="174dp"
tools:layout_editor_absoluteY="187dp"
android:contentDescription="@string/app_name"/>
<Button
android:id="@+id/button"
android:layout_width="127dp"
android:layout_height="wrap_content"
android:layout_x="63dp"
android:layout_y="437dp"
android:text="Previous"
tools:layout_editor_absoluteX="80dp"
tools:layout_editor_absoluteY="429dp" />
<Button
android:id="@+id/button2"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_x="206dp"
android:layout_y="439dp"
android:text="Next"
tools:layout_editor_absoluteX="285dp"
tools:layout_editor_absoluteY="227dp" />
<TextView
android:id="@+id/textView"
android:layout_width="174dp"
android:layout_height="44dp"
android:layout_x="0dp"
android:layout_y="0dp"
android:text="TextView"
tools:layout_editor_absoluteX="59dp"
tools:layout_editor_absoluteY="29dp" />
</android.widget.AbsoluteLayout>
複製代碼
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.plantsai.myapplicationpic">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
複製代碼
作者:
蔡庭豪
時間:
2018-11-7 23:17
package com.example.houyang.imageviewtest;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
Button next;
Button last;
ImageView pic;
int sp = 1;
int picture[] = new int[6];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Init();
}
final Button.OnClickListener clickListener;
{
clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Button temp = findViewById(v.getId());
if (temp.getId() == R.id.nextone) {
if (sp != 6) {
++sp;
pic.setImageResource(picture[sp - 1]);
} else {
WebView webView = new WebView(MainActivity.this);
webView.loadUrl("https://www.google.com.tw");
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
new AlertDialog.Builder(MainActivity.this)
.setTitle("錯誤訊息")
.setCancelable(false)
.setView(webView)
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}
} else if (temp.getId() == R.id.lastone)
{
if (sp != 0) {
--sp;
pic.setImageResource(picture[sp - 1]);
}
}
}
};
}
public void Init(){
next = findViewById(R.id.nextone);
next.setOnClickListener(clickListener);
last = findViewById(R.id.lastone);
last.setOnClickListener(clickListener);
pic = findViewById(R.id.imageView);
pic.setImageResource(R.drawable.img01);
picture[0] = R.drawable.img01;
picture[1] = R.drawable.img02;
picture[2] = R.drawable.img03;
picture[3] = R.drawable.img04;
picture[4] = R.drawable.img05;
picture[5] = R.drawable.img06;
}
}
複製代碼
作者:
洪振庭
時間:
2018-11-9 21:29
package com.example.student.myapplication;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
int ig[]=new int[6];
int index=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=(ImageView)findViewById(R.id.imageView);
ig[0]=R.drawable.img01;
ig[1]=R.drawable.img02;
ig[2]=R.drawable.img03;
ig[3]=R.drawable.img04;
ig[4]=R.drawable.img05;
ig[5]=R.drawable.img06;
this.setTitle("第"+(index+1)+"/6張");
imageView.setImageResource(ig[0]);
}
public void previous(View view) {
if (index > 0) {
imageView.setImageResource(ig[--index]);
this.setTitle("第" + (index + 1) + "/6張");
}else{
final AlertDialog dialog =new AlertDialog.Builder(MainActivity.this)
.setTitle("錯誤")
.setMessage("這是第一面")
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"確定", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("錯誤", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"錯誤", Toast.LENGTH_SHORT).show();
}
})
.show();
}
}
public void next(View view) {
if (index < ig.length - 1) {
imageView.setImageResource(ig[++index]);
this.setTitle("第" + (index + 1) + "/6張");
}
else{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("錯誤");
builder.setMessage("已經到底了");
WebView webView = new WebView(MainActivity.this);
webView.loadUrl("https://www.google.com.tw");
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
builder.setCancelable(false);
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "確定", Toast.LENGTH_SHORT).show();
}
}); builder.setNegativeButton("錯誤", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "錯誤", Toast.LENGTH_SHORT).show();
}
});
builder.setCancelable(false);
final AlertDialog dialog = builder.show();
}
}
public void left(View view) {
}
}
複製代碼
作者:
張健勳
時間:
2018-11-20 19:31
package com.smcs.yingwu.newalertdialog;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button back;
Button next;
ImageView ImageView;
int num = 1;
TextView textView;
int[] image = new int[6];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init()
{
next = findViewById(R.id.button2);
back = findViewById(R.id.button);
ImageView = findViewById(R.id.imageView);
textView = findViewById(R.id.textView);
image[0] = R.drawable.img01;
image[1] = R.drawable.img02;
image[2] = R.drawable.img03;
image[3] = R.drawable.img04;
image[4] = R.drawable.img05;
image[5] = R.drawable.img06;
ImageView.setImageResource(image[0]);
rename();
next.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(num != 6)
{
num++;
rename();
ImageView.setImageResource(image[num-1]);
}else{
rename();
new AlertDialog.Builder(MainActivity.this)
.setTitle("錯誤訊息")
.setMessage("已經到底了!")
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}
}
});
back.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(num != 1)
{
num--;
rename();
ImageView.setImageResource(image[num-1]);
}else{
rename();
WebView WebView = new WebView(MainActivity.this);
WebView.loadUrl("http://www.google.com");
WebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
new AlertDialog.Builder(MainActivity.this)
.setTitle("錯誤訊息")
.setMessage("已經到底了!")
.setCancelable(false)
.setView(WebView)
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}
}
});
}
public void rename()
{
textView.setText("第"+num+"/6張");
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2