自定義對話框

自定義對話框

佈局文件

<?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:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.a2_.MainActivity">

<Button
    android:text="長按展示對話框"
    android:id="@+id/btn_show"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</RelativeLayout>

自定義對話框佈局

<?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:orientation="vertical">

<TextView
    android:id="@+id/tv_delete"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="10dp"
    android:text="點我刪除"
    android:textSize="14sp"/>

<TextView
    android:id="@+id/tv_top"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="10dp"
    android:text="點我置頂"
    android:textSize="14sp"/>

</LinearLayout>

核心代碼

package com.example.a2_;

import android.app.Dialog;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private Button btn_show;

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

    //初始化控件
    btn_show = (Button) findViewById(R.id.btn_show);

    //設置長按點擊事件
    btn_show.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            showDialog();
            return false;
        }
    });

}

//顯示對話框
private void showDialog() {
    //創建對話框
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    //將自定義的對話框佈局轉化爲View對象
    View view = View.inflate(this, R.layout.dialog_item, null);
    //將view引入到對話框中
    builder.setView(view);
    //構建出對話框
    final AlertDialog dialog = builder.create();
    //創建刪除事件
    view.findViewById(R.id.tv_delete).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "刪除", Toast.LENGTH_SHORT).show();
            dialog.dismiss();
        }
    });

    //創建置頂事件
    view.findViewById(R.id.tv_top).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "置頂", Toast.LENGTH_SHORT).show();
            dialog.dismiss();
        }
    });

    dialog.show();
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章