安卓Andriod使用入门(二十)【颜色对话框】

时间:2022-01-21 22:43:16

伟大的事业不是靠力气、速度和身体的敏捷完成的,而是靠性格、意志和知识的力量完成的。


MainActivity.java代码:

package siso.colordialog;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.Toast;

import siso.cdialoglib.ColorDialog;
import siso.cdialoglib.PromptDialog;


public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void showPromptDialog(View view) {
showPromptDlg();
}

private void showPromptDlg() {
new PromptDialog(this)
.setDialogType(PromptDialog.DIALOG_TYPE_SUCCESS)
.setAnimationEnable(true)
.setTitleText(getString(R.string.success))
.setContentText(getString(R.string.text_data))
.setPositiveListener(getString(R.string.ok), new PromptDialog.OnPositiveListener() {
@Override
public void onClick(PromptDialog dialog) {
dialog.dismiss();
}
}).show();
}

public void showTextDialog(View view) {
ColorDialog dialog = new ColorDialog(this);
dialog.setColor("#8ECB54");
dialog.setAnimationEnable(true);
dialog.setTitle(getString(R.string.operation));
dialog.setContentText(getString(R.string.content_text));
dialog.setPositiveListener(getString(R.string.text_iknow), new ColorDialog.OnPositiveListener() {
@Override
public void onClick(ColorDialog dialog) {
Toast.makeText(MainActivity.this, dialog.getPositiveText().toString(), Toast.LENGTH_SHORT).show();
}
}).show();
}

public void showPicDialog(View v) {
ColorDialog dialog = new ColorDialog(this);
dialog.setTitle(getString(R.string.operation));
dialog.setAnimationEnable(true);
dialog.setAnimationIn(getInAnimationTest(this));
dialog.setAnimationOut(getOutAnimationTest(this));
dialog.setContentImage(getResources().getDrawable(R.mipmap.sample_img));
dialog.setPositiveListener(getString(R.string.delete), new ColorDialog.OnPositiveListener() {
@Override
public void onClick(ColorDialog dialog) {
Toast.makeText(MainActivity.this, dialog.getPositiveText().toString(), Toast.LENGTH_SHORT).show();
}
})
.setNegativeListener(getString(R.string.cancel), new ColorDialog.OnNegativeListener() {
@Override
public void onClick(ColorDialog dialog) {
Toast.makeText(MainActivity.this, dialog.getNegativeText().toString(), Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}).show();
}

public void showAllModeDialog(View view) {
ColorDialog dialog = new ColorDialog(this);
dialog.setTitle(getString(R.string.operation));
dialog.setAnimationEnable(true);
dialog.setContentText(getString(R.string.content_text));
dialog.setContentImage(getResources().getDrawable(R.mipmap.sample_img));
dialog.setPositiveListener(getString(R.string.delete), new ColorDialog.OnPositiveListener() {
@Override
public void onClick(ColorDialog dialog) {
Toast.makeText(MainActivity.this, dialog.getPositiveText().toString(), Toast.LENGTH_SHORT).show();
}
})
.setNegativeListener(getString(R.string.cancel), new ColorDialog.OnNegativeListener() {
@Override
public void onClick(ColorDialog dialog) {
Toast.makeText(MainActivity.this, dialog.getNegativeText().toString(), Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}).show();
}

public static AnimationSet getInAnimationTest(Context context) {
AnimationSet out = new AnimationSet(context, null);
AlphaAnimation alpha = new AlphaAnimation(0.0f, 1.0f);
alpha.setDuration(150);
ScaleAnimation scale = new ScaleAnimation(0.6f, 1.0f, 0.6f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scale.setDuration(150);
out.addAnimation(alpha);
out.addAnimation(scale);
return out;
}

public static AnimationSet getOutAnimationTest(Context context) {
AnimationSet out = new AnimationSet(context, null);
AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f);
alpha.setDuration(150);
ScaleAnimation scale = new ScaleAnimation(1.0f, 0.6f, 1.0f, 0.6f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scale.setDuration(150);
out.addAnimation(alpha);
out.addAnimation(scale);
return out;
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_horizontal_margin"
android:orientation="vertical">


<Button
android:onClick="showPromptDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/prompt_dialog"/>


<Button
android:onClick="showPicDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/pic"/>


<Button
android:onClick="showTextDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/text"/>


<Button
android:onClick="showAllModeDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/text_and_pic"/>


</LinearLayout>

strings.xml

<resources>
<string name="app_name">颜色对话框</string>
<string name="operation">操作</string>
<string name="content_text">这是内容消息</string>
<string name="delete">删除</string>
<string name="cancel">取消</string>
<string name="text_iknow">我知道</string>
<string name="ok">ok</string>
<string name="success">成功</string>
<string name="help">帮助</string>
<string name="info">信息</string>
<string name="error">错误</string>
<string name="warning">警告</string>
<string name="text_data">您的信息文本放在这里</string>
<string name="prompt_dialog">提示对话框</string>
<string name="pic">图片</string>
<string name="text">文本</string>
<string name="text_and_pic">文本和图片</string>
</resources>

库项目结构如图:

安卓Andriod使用入门(二十)【颜色对话框】


项目运行结果如图:

安卓Andriod使用入门(二十)【颜色对话框】


安卓Andriod使用入门(二十)【颜色对话框】


安卓Andriod使用入门(二十)【颜色对话框】


安卓Andriod使用入门(二十)【颜色对话框】


安卓Andriod使用入门(二十)【颜色对话框】