Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android實現好看的自定義提示框

android實現好看的自定義提示框

編輯:關於Android編程

效果圖:
這裡寫圖片描述

代碼部分

main_activity.xml



    

    

MainActivity.java:

package com.example.mydialog;

import android.app.Activity;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import dialog.MessageFragment;


public class MainActivity extends ActionBarActivity {
    //提示框fragment
    private MessageFragment mMessageFragment;
    //bundle用來傳遞提示框的信息
    private Bundle bundle;
    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=(Button)findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showdialog();
            }
        });
    }
    private void showdialog() {
        if (mMessageFragment == null) {
            mMessageFragment = new MessageFragment();
        }
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putString(content, 是否確認收件);
        bundle.putString(title, 標題);
        mMessageFragment.setOnResultListener(new OnItemMsgResultListener());
        mMessageFragment.setArguments(bundle);
        // 顯示提示窗口
        mMessageFragment.show(getFragmentManager(), );
    }

    class OnItemMsgResultListener implements MessageFragment.OnMsgResultListener {

        public OnItemMsgResultListener() {
        }

        @Override
        public void onResultFun(int resultCode) {
            if (resultCode == Activity.RESULT_OK) {
                Toast.makeText(MainActivity.this,確認,Toast.LENGTH_SHORT).show();
                // getAliasData();
            }else{
                Toast.makeText(MainActivity.this,取消,Toast.LENGTH_SHORT).show();
            }
        }
    }

}

提示框的布局文件:

message_dialog.xml:



    
        
    
    
        
    
    

drawable下的xml文件:

button_msg_cancel.xml:



    
    
    

button_msg_submit.xml:



    
    
    

其他的png圖片資源在鏈接:http://download.csdn.net/detail/u010778159/8923083

最後,

MessageFragment.java:

package dialog;

import android.app.Activity;
import android.app.DialogFragment;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import com.example.mydialog.R;

public class MessageFragment extends DialogFragment {

    public static final String TAG = MessageDialog;
    private OnMsgResultListener mOnMsgResultListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        //message_dialog是提示框的布局
        View show_view = inflater.inflate(R.layout.message_dialog, null);

        TextView mTxtContent = (TextView) show_view.findViewById(R.id.tv_content);
        TextView mTxtTitle = (TextView) show_view.findViewById(R.id.tv_title);
        Button btnSubmit = (Button) show_view.findViewById(R.id.btn_submit);
        Button btnCancel = (Button) show_view.findViewById(R.id.btn_cancel);
        Bundle bundle = this.getArguments();
        if(bundle != null){
            String title = bundle.getString(title);
            if(title!=null){
                mTxtTitle.setText(bundle.getString(title));
            }
            mTxtContent.setText(bundle.getString(content));
        }
        btnSubmit.setOnClickListener(btnClickListener);
        btnCancel.setOnClickListener(btnClickListener);

        setCancelable(false);
        return show_view;
    }

    public void setOnResultListener(OnMsgResultListener listener) {
        mOnMsgResultListener = listener;
    }

    View.OnClickListener btnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_submit:
                    mOnMsgResultListener.onResultFun(Activity.RESULT_OK);
                    dismiss();
                    break;

                case R.id.btn_cancel:
                    mOnMsgResultListener.onResultFun(Activity.RESULT_CANCELED);
                    dismiss();
                    break;
                default:
                    break;
            }
        }
    };

    public interface OnMsgResultListener {
        void onResultFun(int resultCode);
    }
}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved