2017-03-19 15 views
0

次のユースケースでは、Androidで何かをするためにあなたの助けが必要です。 私はそのレイアウトであるアンドロイド、カスタムダイアログを作成しました: dialog_threshold.xmlAndroidのカスタムダイアログにコンポーネントを動的に追加する

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="20dp"> 
    <ImageView 
     android:src="@drawable/ic_media_route_on_03_dark" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scaleType="center" 
     android:background="#FFFFBB33" 
     android:contentDescription="@string/set_target" /> 

    <Spinner 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/spinner_threshold" /> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/layout_threshold"> 

     <TextView 
      android:text="" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/targetSelected" 
      android:layout_weight="1" /> 

     <Spinner 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/threshold_operator_spinner" 
      android:layout_weight="1" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:inputType="textPersonName" 
      android:text="" 
      android:ems="10" 
      android:id="@+id/threshold_val" 
      android:layout_weight="1" /> 

    </LinearLayout> 

</LinearLayout> 

と活動で、私は特定のボタンをクリックしたとき、私はカスタムを作成するには、このようなものを持っていますダイアログ:

setThreshold_button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       AlertDialog.Builder mBuilder = new AlertDialog.Builder(DiseaseActivity.this); 
       //Inflate the custom layout 
       View mView = getLayoutInflater().inflate(R.layout.dialog_threshold, null); 

       //Set title for dialog 
       mBuilder.setTitle("Set thresholds for your target"); 
       //Define the spinner inside your custom layout 
       final Spinner mSpinner = (Spinner) mView.findViewById(R.id.spinner_threshold); //because it doesn't exist in the main layout, but only in the custom layout 
       //Define the ArrayAdapter 
       ArrayAdapter<String> adapter = new ArrayAdapter<String>(DiseaseActivity.this, 
         android.R.layout.simple_spinner_item, 
         getResources().getStringArray(R.array.threshold_choices)); 
       adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
       mSpinner.setAdapter(adapter); 

       if (!mSpinner.getSelectedItem().toString().equalsIgnoreCase("Choose a threshold option")) { 
        String ThresholdSelection = mSpinner.getSelectedItem().toString(); 
       } 
       mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
        @Override 
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
         if ((dialog2 != null) && dialog2.isShowing() && !mSpinner.getSelectedItem().toString().equalsIgnoreCase("Choose a threshold option")) { 
          dialog2.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true); 

         } else { 
          dialog2.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); 
         } 
        } 

        @Override 
        public void onNothingSelected(AdapterView<?> parent) { 

        } 
       }); 

       //Set the positive and negative button for the custom dialog 
       mBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialogInterface, int i) { 
         disease.setThreshold(mSpinner.getSelectedItem().toString()); 

         if (!mSpinner.getSelectedItem().toString().equalsIgnoreCase("Choose a threshold option")) { 
          Toast.makeText(DiseaseActivity.this, 
            mSpinner.getSelectedItem().toString(), 
            Toast.LENGTH_SHORT) 
            .show(); 
          setInterval_button.setEnabled(true); 
         } 
        } 
       }); 
       mBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialogInterface, int i) { 
         dialogInterface.dismiss(); 
        } 
       }); 
       mBuilder.setView(mView); 
       dialog2 = mBuilder.create(); 
       dialog2.show(); 
       dialog2.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); 

      } 
     }); 

私は今、何をしたいかは、上記のダイアログに(のEditTextまたはのTextViewなど)動的に他のコンポーネントを追加することである前にそれが示されています。 どうすればいいですか?

ありがとうございます!

答えて

2

カスタムビューを作成する>>親(ビューグループを追加する)>>>を追加して追加します。 例: -

mView.addView(new TextView(ActivityContext),LayoutParamss); 
mBuilder.setView(mView); 
+0

こんにちは、お返事ありがとうございます。これら2行のコードを追加するだけで動作するのですか?詳細を教えてください。 – Giulio

+0

申し訳ありませんでした - 私を助けてくれますか? – Giulio

+0

@Giulio ..つまり、あなたは 'View' first.eg:-' EditText edit = new EditText(context); 'やTextViewかwhtvrを作成する必要があります。親ビューグループを見つけるよりも(' - ' mView.findViewById')ビューを追加します。単に 'yourParentView.addView(あなたの新しいビュー)を呼び出す;'。それでおしまい。 –

関連する問題