0

TabbedJavaにタブ付きアクティビティがあります ボタンにカスタムダイアログを表示したい場合クリックします。 (カスタムダイアログのために、私は名前の別のXMLファイルがあります。dg_selct.xml)を コード:ここでタブ付きアクティビティでカスタムダイアログを表示できません

public class Tab2 extends Fragment { 

    Button btn; 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 

     View v = inflater.inflate(R.layout.tab2, container, false); 


     btn=(Button)v.findViewById(R.id.button2); 
     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       final AlertDialog.Builder dialog = new AlertDialog.Builder(getContext()); 
       View bView = getLayoutInflater().inflate(R.layout.dg_selct,null); 

       dialog.setView(bView); 
       AlertDialog customDialog = dialog.create(); 
       customDialog.setTitle("Search"); 
       customDialog.show(); 

      } 
     }); 

dg_selct.xmlです:また

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <RelativeLayout 
     android:id="@+id/relativeLayout2" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_marginBottom="8dp" 
     android:layout_marginEnd="8dp" 
     android:layout_marginStart="8dp" 
     android:layout_marginTop="8dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintEnd_toEndOf="parent" 
     app:layout_constraintHorizontal_bias="1.0" 
     app:layout_constraintStart_toStartOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintVertical_bias="1.0"> 

     <CheckBox 
      android:id="@+id/chkpg" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentStart="true" 
      android:layout_below="@+id/textView4" 
      android:layout_marginStart="63dp" 
      android:layout_marginTop="27dp" 
      android:checked="true" 
      android:text="@string/clspg" 
      android:textSize="23sp" /> 

     <TextView 
      android:id="@+id/textView4" 

      android:layout_width="250dp" 
      android:layout_height="wrap_content" 
      android:layout_alignParentEnd="true" 
      android:layout_alignParentTop="true" 
      android:layout_marginEnd="40dp" 
      android:layout_marginTop="42dp" 
      android:text="Mark the Groups, You want to Send the Message:" 
      android:textColor="#000000" 
      android:textSize="17sp" /> 

     <CheckBox 
      android:id="@+id/chknur" 
      android:checked="true" 

      android:textSize="23sp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignStart="@+id/chkpg" 
      android:layout_below="@+id/chkpg" 
      android:layout_marginTop="32dp" 
      android:text="@string/clsn" /> 

     <CheckBox 
      android:id="@+id/chkprep" 
      android:checked="true" 
      android:textSize="23sp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignStart="@+id/chknur" 
      android:layout_below="@+id/chknur" 
      android:layout_marginTop="30dp" 
      android:text="@string/clsp" /> 

     <CheckBox 
      android:id="@+id/chkone" 
      android:checked="true" 
      android:textSize="23sp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignStart="@+id/chkprep" 
      android:layout_below="@+id/chkprep" 
      android:layout_marginTop="35dp" 
      android:text="@string/cls1" /> 

     <CheckBox 
      android:checked="true" 
      android:id="@+id/chktwo" 
      android:textSize="23sp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignStart="@+id/chkone" 
      android:layout_below="@+id/chkone" 
      android:layout_marginTop="24dp" 
      android:text="@string/cls2" /> 

     <CheckBox 
      android:id="@+id/chkthree" 
      android:checked="true" 
      android:textSize="23sp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignStart="@+id/chktwo" 
      android:layout_below="@+id/chktwo" 
      android:layout_marginTop="26dp" 
      android:text="@string/cls3" /> 

     <CheckBox 
      android:id="@+id/chkfour" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignStart="@+id/chkthree" 
      android:layout_below="@+id/chkthree" 
      android:checked="true" 
      android:layout_marginTop="30dp" 
      android:text="@string/cls4" 
      android:textSize="23sp" /> 

     <CheckBox 
      android:id="@+id/chkfive" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_above="@+id/chknur" 
      android:layout_alignEnd="@+id/textView4" 
      android:layout_marginEnd="32dp" 
      android:checked="true" 
      android:text="@string/cls5" 
      android:textSize="23sp" /> 

     <CheckBox 
      android:id="@+id/chksix" 
      android:textSize="23sp" 
      android:checked="true" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignStart="@+id/chkfive" 
      android:layout_alignTop="@+id/chknur" 
      android:text="@string/cls6" /> 

     <CheckBox 
      android:id="@+id/chksvn" 
      android:checked="true" 
      android:textSize="23sp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignStart="@+id/chksix" 
      android:layout_alignTop="@+id/chkprep" 
      android:text="@string/cls7" /> 

     <CheckBox 
      android:id="@+id/chknine" 
      android:textSize="23sp" 
      android:checked="true" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignStart="@+id/chksvn" 
      android:layout_alignTop="@+id/chkone" 
      android:text="@string/cls9" /> 

     <CheckBox 
      android:id="@+id/chkten" 
      android:textSize="23sp" 
      android:checked="true" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignStart="@+id/chknine" 
      android:layout_alignTop="@+id/chktwo" 
      android:text="@string/cls10" /> 

     <CheckBox 
      android:id="@+id/chkextra" 
      android:checked="true" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignStart="@+id/chkten" 
      android:layout_alignTop="@+id/chkthree" 
      android:text="@string/clsextra" 
      android:textSize="23sp" /> 
    </RelativeLayout> 
</android.support.constraint.ConstraintLayout> 

、私が使用している場合代わりにfinal AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());final AlertDialog.Builder dialog = new AlertDialog.Builder(Tab2.this);、コンパイラはエラーを与える:"Builderのビルダー(android.Content.context)はcom.abc.Tab2に適用することはできません"

dg_select.xmlには、いくつかのチェックボックスとTextViewがあります。しかし、ダイアログが表示されているとき、私は上記のコードで述べたタイトルのみを表示します。 このダイアログでdg_selct.xmlを拡張するにはどうすればよいですか?

+0

あなたのカスタムレイアウト** dg_selct **を質問 – Prem

+1

で共有しようとしましたあなたのAlertDialog.Builder(getContext());を 'AlertDialog.Builder(getActivity());'に変更しようとしましたか? – Jerrol

+0

は、 'getContext()'と 'getActivity()'の両方を試しました まだ成功しません –

答えて

1

Dialog代わりのAlertDialog

ここ
Dialog dialog = new Dialog(getActivity()); 
dialog.setContentView(R.layout.dg_selct); 

// to bind your control from dialog like this 
final TextView textView= (TextView) dialog.findViewById(R.id.YourTextBiew); 

final CheckBox checkBox= (CheckBox) dialog.findViewById(R.id.YourCheckBox); 

final Button btnShow = (Button) findViewById(R.id.btnShow); 

textView.setText("PREM"); 
dialog.setTitle("Search"); 
dialog.show(); 

UPDATE

change Your RelativeLayout hight & width to match_parent OR wrap_content like below code

<RelativeLayout 
     android:id="@+id/relativeLayout2" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
+0

まだタイトルが表示されています そして:textViewのオブジェクトは "使用されていません"と答えています –

+0

ダイアログのカスタムレイアウトを共有し、 @ZubairYounas – Prem

+0

レイアウトファイルが共有されています。 setTextが使用されていますが、ダイアログには表示されません。 ダイアログはまだタイトルのみで構成されています –

1

この使用してみてください、私はあなたが設定したdg_select.xmlのあなたのXMLを見ましたはるかRelativeLayoutの幅と幅は0dpです。そのため、チェックボックスを表示できません。 RelativeLayout高さまたは幅wrap_contentまたはmatch_parentを設定する必要があります。

AlertDialog

はfragment.Soの参照を取ることができない理由です 活動の一部であり、あなたはあなたのアクティビティの参照をAlertDialogあなたがしなければならない初期化します。

+0

はい。それが問題でした。 コンパイラitslefが高さと0を設定する理由を知らない/ –

+0

高さまたは幅を設定するために必要なweightsumとweightを使用すると0dp –

関連する問題