2012-04-08 13 views
2

Ich habe einen Dialog, der mit diesem Code erstellt wird:Sichtbarkeit der Textview in Dialog

final Dialog dialog1 = new Dialog(Test.this); 
dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE); 
dialog1.setContentView(R.layout.test_layout); 
dialog1.setCancelable(true); 
... 

Die Layoutdatei 'test_layout.xml' enthält eine typische Textview:

<TextView 
    android:id="@+id/tv_username" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingLeft="10dp" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:text="Test" /> 

Ich mag der Code, um diesen TextView in einigen Fällen unsichtbar (weg) zu setzen. Ich habe versucht, folgende:

TextView tv = (TextView) dialog1.findViewById(R.id.tv_username); 
tv.setVisibility(TextView.GONE); 

Aber die Textview erscheint noch im Dialog. Wenn ich in der Layout-XML-Datei android: Sichtbarkeit = "weg" setze, erscheint es nicht im Dialog. Aber ich muss es über Code tun.

Hier wird der gesamte Code:

if (whichButton == 1) { 

      final Dialog dialog1 = new Dialog(Test.this); 
             dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE); 
      dialog1.setContentView(R.layout.test_layout); 
      dialog1.setCancelable(true); 

      TextView tv = (TextView) dialog1.findViewById(R.id.tv_username); 
      tv.setVisibility(View.GONE); 


     ... 
     dialog1.show(); 
} 

Hier die gesamte XML-Datei:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="250dp" 
    android:layout_height="match_parent" 
> 
    <TextView 
     android:id="@+id/tv_name" 
     android:paddingTop="10dp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="15dp" 
     android:textSize="14dp" 
     android:text="Name:" /> 
    <EditText 
     android:id="@+id/username" 
     android:hint="@string/enterName" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:maxLength="15" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:scrollHorizontally="true" 
     android:capitalize="words" 
     android:singleLine="true" 
     android:gravity="fill_horizontal" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 
    <TextView 
     android:id="@+id/tv_username" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="10dp" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Test" /> 
    <TextView 
     android:id="@+id/tv_message" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="15dp" 
     android:textSize="14dp" 
     android:text="@string/message" /> 
    <EditText 
     android:id="@+id/tip" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:maxLength="290" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:scrollbars="vertical" 
     android:inputType="textMultiLine" 
     android:capitalize="words" 
     android:gravity="fill_horizontal" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 
    <Button 
     android:id="@+id/button" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/send_message" /> 
</LinearLayout> 
+0

versuchen Sie tv.setVisibility (View.GONE); – Shubhayu

+0

Dies funktioniert auch nicht. Die Textansicht ist weiterhin sichtbar. Kann das "Finale" von Dialog das Problem sein? – tobias

+0

Ich denke, das "Finale" bedeutet im Grunde, dass Sie keine Aufgaben ändern können, aber kein Problem mit setVisibility() sein sollte, aber Sie könnten es einmal versuchen. Und wenn Sie dies in einem Listener tun und es Sie nicht tun lassen, machen Sie dialog1 vorerst zu einer globalen Variable. – Shubhayu

Antwort

0

fand ich eine Lösung oder Abhilfe.

Der Dialog wurde in der OnClick-Methode eines AlertDialogs erstellt.

Jetzt verwende ich die showDialog() -Methode und handle es in der entsprechenden Callback-Methode. Da funktioniert es gut.

Ich verstehe nicht, warum das funktioniert, aber ich möchte nur anderen Benutzern sagen, wie sie dieses Problem lösen können.

Ich würde mich freuen, wenn jemand mit einer Klärung kommen kann.

1

Versuchen tv.setVisibility(View.INVISIBLE);

+0

das funktioniert auch nicht. Und ich muss GONE verwenden, weil ich möchte, dass der Raum auch entfernt werden soll. – tobias

+0

Warum werfen Sie es in Ihren Dialog? Ich meine, warum machst du das? TextView tv = (Textansicht) dialog1.findViewById (R.id.tv_username); Verwenden Sie stattdessen tv = (TextView) findViewById (R.id.tv_username) ... –

+0

es funktioniert auch nicht mit tv = (TextView) findViewById (R.id.tv_username) – tobias