2016-02-09 21 views
9

मैं अपने संवाद में कस्टम शीर्षक जोड़ने की कोशिश कर रहा हूं, हालांकि जब भी मैं अपना एप्लिकेशन चलाता हूं तो यह शीर्षक नहीं दिखाता है।Dialog.setTitle शीर्षक नहीं दिखा रहा है

संवाद बनाने के लिए मेरे कोड

final Dialog passwordDialog = new Dialog(this); 
passwordDialog.setContentView(R.layout.admin_password_dialog); 
passwordDialog.setTitle("Enter An Administrative Password"); 
passwordDialog.show(); 

है और मेरे लेआउट फ़ाइल

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

<Button 
    android:id="@+id/btn_confirmPassword" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentEnd="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentStart="true" 
    android:layout_below="@+id/edit_adminPassword" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:text="@string/confirmPassword"/> 

<EditText 
    android:id="@+id/edit_adminPassword" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentEnd="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:ems="10" 
    android:inputType="textPassword"/> 

है और यहाँ मैं क्या हो रही है है

Here is what I am getting

क्या मुझे कुछ याद आ रही है?

+0

के निर्माता http://stackoverflow.com/questions/4852573/how- को यह शैली पारित टू-एड-टाइटल-टू-द-कस्टम-डायलॉग आपको एक वर्कअराउंड दे सकता है – Gavriel

उत्तर

8

अन्य जवाब की तरह, लेकिन अधिक संक्षिप्त

final AlertDialog diag = new AlertDialog.Builder(this) 
     .setTitle("Enter An Administrative Password") 
     .setView(R.layout.admin_password_dialog) 
     .create(); 

diag.show(); 

Button diagButton = (Button) diag.findViewById(R.id.btn_confirmPassword); 
diagButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     // handle button click 
     EditText input = (EditText) diag.findViewById(R.id.edit_adminPassword); 
     String s = input.getText().toString(); 
    } 
}); 
+1

@RyanNewman के बिना पूर्ण स्क्रीन लेता है - आप 'android.support.v7.app.AlertDialog' का उपयोग कर सकते हैं। यदि आप मेरा जवाब देखते हैं, तो यह भी अधिक समावेशी है, जैसे कि बटन –

+0

से बटन और एडिटटेक्स्ट कैसे प्राप्त करें, जब मैं ऑनक्लिकलिस्टर जोड़ने का प्रयास करता हूं तो मुझे एक शून्य ऑब्जेक्ट रिफ़र्न्स मिल रहा है। तुम जानते हो क्यों? –

+0

ठीक है, अगर यह आईडी नहीं मिल पाती है तो देखें ViewById शून्य वापस आती है। मैंने ईमानदारी से इसका परीक्षण नहीं किया, लेकिन मुझे लगता है कि यह 'सेट व्यू' –

2

तुम सिर्फ एक Dialog बनाने के बजाय एक AlertDialog.Builder उपयोग करना चाहिए:

// 1. Instantiate an AlertDialog.Builder with its constructor 
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

// 2. Chain together various setter methods to set the dialog characteristics 
builder.setView(R.layout.admin_password_dialog); 
builder.setTitle("Enter An Administrative Password"); 

// 3. Get the AlertDialog from create() 
AlertDialog dialog = builder.create(); 
dialog.show(); 

Android डेवलपर गाइड पर संवाद के लिए here देखें।

+0

धन्यवाद, इसे बदल दिया;) –

+0

मुझे यकीन नहीं था कि 'सेट' विधियों के लिए बिल्डर को पुन: असाइन करना आवश्यक था, इसलिए मैंने अपना जवाब दिया , बस मामले में :) –

+0

दोनों तरीकों से किया जा सकता है :) –

3

साथ ही आप इस विधि कोशिश करते हैं और इस्तेमाल किया विषय पर आधारित अलग दृष्टिकोण शैलियों मिल सकती है।

<style name="FilterDialogTheme" parent="@android:style/Theme.Holo.Light.Dialog"> 
    <item name="android:windowNoTitle">false</item> 
</style> 

संवाद निर्माता

public FilterDialog(Context context) { 
     super(context, R.style.FilterDialogTheme); 
    } 

अपनी परियोजना के लिए उपयोग @style/Theme.Appcompat.Light.Dialog में।

12

आप इस तरह अपनी शैली को परिभाषित करना चाहिए:

<style name="Dialog" parent="Theme.AppCompat.Dialog"> 
    <item name="android:windowNoTitle">false</item> 
    <item name="android:windowIsFloating">true</item> 
</style> 

और फिर संवाद

final Dialog passwordDialog = new Dialog(this,R.style.Dialog); 
+2

इसे एक समाधान कहा जाता है, जो वार्तालाप के बजाय 'अलर्टडिअलॉग' का उपयोग करने का सुझाव देता है समाधान नहीं है, यह एक काम हो सकता है ... – kAmol

संबंधित मुद्दे