簡體   English   中英

在Android中創建自定義對話框

[英]Create custom dialog box in Android

我發現所有與警報框,對話框都兼容的東西,但是當我嘗試使用自己的自定義對話框創建東西時,這給我帶來了麻煩。 盡管我按照開發指南的指示進行了操作: http : //developer.android.com/intl/de/guide/topics/ui/dialogs.html我無法達到我的結果,只是它顯示了與以下錯誤消息。

03-04 11:37:08.780: ERROR/AndroidRuntime(726): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

我已經嘗試制作自定義對話框很多天了,但是我無法提出它。 我什至嘗試了在論壇上獲得的解決方案,但似乎也沒有用。 給我一些不錯的代碼或建議以配合使用...關於此的任何建議都是可取的。

Android對話框-困惑地看了一下這個問題,看起來與您的問題相似。 另外,您還必須共享一個導致錯誤的代碼,否則很難提供幫助。

創建custom_dialog xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:id="@+id/root"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Custom Dialog"
        android:textColor="#000"
        android:textSize="25dp"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_height="wrap_content">
    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ok"
        android:textColor="#000"
        android:textSize="19dp"/>
        <Button
            android:id="@+id/cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cancel"
            android:textColor="#000"
            android:textSize="19dp"/>
    </LinearLayout>
</RelativeLayout>

在MainActivity.java中添加自定義對話框視圖

package techamongus.com.testapplication;

import android.app.Activity;
import android.app.Dialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {
Dialog customDialog;
    Button ok,cancel;
    Button showDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showDialog=(Button)findViewById(R.id.show);
        showDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customDialog.show();
            }
        });
        customDialog=new Dialog(this);
        LayoutInflater customInflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
        View customLayout=customInflater.inflate(R.layout.custom_dialog, (ViewGroup) findViewById(R.id.root));
        customDialog.setContentView(customLayout);
        ViewGroup.LayoutParams layoutParams2= customLayout.getLayoutParams();
        layoutParams2.height=400;
        ok=(Button)customLayout.findViewById(R.id.ok);
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //done what do you want to do
                customDialog.dismiss();
            }
        });
        cancel=(Button)customLayout.findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //done what do you want to do
                customDialog.dismiss();
            }
        });
    }

}

這是您的main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
    tools:context="techamongus.com.testapplication.MainActivity">
    <Button
        android:id="@+id/show"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:text="Show dialog"
        android:layout_gravity="center"
        android:textColor="#000"/>
</LinearLayout>

http://www.techamongus.com/2017/03/android-create-custom-dialog-program.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM