標題:
AlertDialog
[打印本頁]
作者:
周政輝
時間:
2018-10-20 13:37
標題:
AlertDialog
改寫主題 ImageView 的程式碼,在畫面左下方新增一按鈕 "結束",alpha 值設 0.8,使其呈現顏色稍暗的效果。當點擊 "結束" 時,會跳出包含 "確定" 與 "取消" 的 AlertDialog 確認視窗。
[attach]5073[/attach]
[attach]5074[/attach]
作者:
黃璽安
時間:
2018-10-27 12:11
package com.example.student.myapplication;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private int imgId[] = {R.drawable.img01, R.drawable.img02, R.drawable.img03, R.drawable.img04, R.drawable.img05, R.drawable.img06};
private Button btn1, btn2, end;
private ImageView imageView;
private int p = 0, count = imgId.length;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("第 " + (p + 1) + " / " + count + " 張");
btn2 = (Button) findViewById(R.id.button2);
btn1 = (Button) findViewById(R.id.button1);
end = (Button) findViewById(R.id.end);
imageView = (ImageView) findViewById(R.id.imageView);
btn1.setOnClickListener(myListener);
btn2.setOnClickListener(myListener);
end.setOnClickListener(myListener);
}
public View.OnClickListener myListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
p--;
if (p < 0)
p = count - 1;
imageView.setImageResource(imgId[p]);
setTitle("第 " + (p + 1) + " / " + count + " 張");
break;
case R.id.button2:
p++;
if (p == count)
p = 0;
imageView.setImageResource(imgId[p]);
setTitle("第 " + (p + 1) + " / " + count + " 張");
break;
case R.id.end:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("確認視窗");
builder.setIcon(R.mipmap.ic_launcher);
builder.setMessage("即將離開應用程式!");
builder.setPositiveButton("確認", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
builder.setCancelable(false);
break;
}
}
};
}
複製代碼
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="#000000">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一頁"
android:id="@+id/button2"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="172dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上一頁"
android:id="@+id/button1"
android:layout_alignTop="@+id/button2"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignEnd="@+id/button2"
android:layout_alignParentStart="true"
android:layout_above="@+id/button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="結束"
android:id="@+id/end"
android:layout_below="@+id/button1"
android:layout_alignParentStart="true"
android:layout_marginTop="46dp" />
</RelativeLayout>
複製代碼
作者:
洪振庭
時間:
2018-11-2 22:21
package com.example.student.myapplication;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private int Id[] = {R.drawable.img01, R.drawable.img02, R.drawable.img03, R.drawable.img04, R.drawable.img05, R.drawable.img06};
private Button btn1, btn2, end;
private ImageView imageView;
private int n = 0, cnt = Id.length;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("第 " + (n + 1) + " / " + cnt + " 張");
btn2 = (Button) findViewById(R.id.button2);
btn1 = (Button) findViewById(R.id.button1);
end = (Button) findViewById(R.id.end);
imageView = (ImageView) findViewById(R.id.imageView);
btn1.setOnClickListener(myListener);
btn2.setOnClickListener(myListener);
end.setOnClickListener(myListener);
}
public View.OnClickListener myListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
n--;
if (n < 0)
n = cnt - 1;
imageView.setImageResource(Id[n]);
setTitle("第 " + (n + 1) + " / " + cnt + " 張");
break;
case R.id.button2:
n++;
if (n == cnt)
n = 0;
imageView.setImageResource(Id[n]);
setTitle("第 " + (n + 1) + " / " + cnt + " 張");
break;
case R.id.end:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("確認視窗");
builder.setIcon(R.mipmap.ic_launcher);
builder.setMessage("即將離開應用程式!");
builder.setPositiveButton("確認", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
builder.setCancelable(false);
break;
}
}
};
}
複製代碼
作者:
蔡庭豪
時間:
2018-11-3 11:12
package com.example.student.myapplication_20181103;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivityimage 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_activityimage);
Init();
}
Button.OnClickListener clicklistener = new Button.OnClickListener() {
@Override
public void onClick(View v) {
Button temp = (Button)findViewById(v.getId());
if(temp.getId() == R.id.next){
if(sp != 6) {
++sp;
pic.setImageResource(picture[sp - 1]);
}
else{
new AlertDialog.Builder(MainActivityimage.this)
.setTitle("錯誤訊息")
.setMessage("到底了")
.setCancelable(false)
.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.last){
if(sp != 0 ) {
sp--;
pic.setImageResource(picture[sp - 1]);
}
else{
new AlertDialog.Builder(MainActivityimage.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();
}
}
}
};
public void Init(){
next = (Button)findViewById(R.id.next);
next.setOnClickListener(clicklistener);
last = (Button)findViewById(R.id.last);
last.setOnClickListener(clicklistener);
pic = (ImageView)findViewById(R.id.pic);
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-3 11:12
本帖最後由 李知易 於 2018-11-9 21:12 編輯
package com.example.user.hw20181021;
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.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
int image[]=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);
this.setTitle("第"+(index+1)+"/6張");
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]);
}
public void previous(View view) {
if (index > 0) {
imageView.setImageResource(image[--index]);
this.setTitle("第" + (index + 1) + "/6張");
}else{
final AlertDialog dialog =new AlertDialog.Builder(MainActivity.this)
.setTitle("Error")
.setMessage("This is the first page!")
.setCancelable(false)
.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"Okay", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"Cancel", Toast.LENGTH_SHORT).show();
}
})
.show();
}
}
public void next(View view) {
if (index < image.length - 1) {
imageView.setImageResource(image[++index]);
this.setTitle("第" + (index + 1) + "/6張");
}else{
final AlertDialog dialog =new AlertDialog.Builder(MainActivity.this)
.setTitle("Error")
.setMessage("It is already the last page!")
.setCancelable(false)
.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Okay", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Cancel", Toast.LENGTH_SHORT).show();
}
})
.setCancelable(false)
.show();
}
}
public void left(View view) {
}
}
複製代碼
作者:
李知易
時間:
2018-11-3 11:13
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ray.ap.hw1071021.MainActivity"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
app:srcCompat="@drawable/img01"
android:id="@+id/imageView"
/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="0,1" >
<TableRow>
<Button
android:text="上一頁"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:onClick="previous" />
<Button
android:text="下一頁"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:layout_alignTop="@+id/button"
android:onClick="next" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="結束"
android:id="@+id/button3"
android:layout_column="0"
android:onClick="left" />
</TableRow>
</TableLayout>
</LinearLayout>
複製代碼
作者:
張健勳
時間:
2018-11-20 19:25
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.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();
new AlertDialog.Builder(MainActivity.this)
.setTitle("錯誤訊息")
.setMessage("已經到底了!")
.setCancelable(false)
.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