2017-03-19 19 views
0

다음 사용 사례에서 Android에서 뭔가 도움이 필요합니다. 나는 특정 버튼을 클릭 할 때Android의 사용자 정의 대화 상자에 동적으로 구성 요소를 추가하십시오.

<?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> 

그리고 활동에이, 내가 정의를 만들려면이 같은 뭔가를 dialog_threshold.xml : 나는 누구의 레이아웃 안드로이드에서 사용자 지정 대화 상자를 만들었습니다 대화 상자 :

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); 

      } 
     }); 

내가 지금하고 싶은 것은 전에 대화 상자 위의 도시에 (글고 또는 텍스트 뷰 등) 동적으로 다른 구성 요소를 추가하는 것입니다. 어떻게해야합니까?

미리 감사드립니다.

답변

2

그냥 사용자 정의보기 만들기 >> (추가 할보기 그룹에서) 부모를 찾아서 >>> 추가하십시오. 예 : -

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

안녕하십니까. 이 두 줄의 코드 만 추가하면됩니까? 좀 더 자세히 알려주시겠습니까? – Giulio

+0

미안하다는 것을 이해하지 못했습니다 - 저를 도울 수 있습니까? – Giulio

+0

@Giulio .. 당신이 부모보기 그룹을 찾는다면 (예 : -'Edit'Text edit = new EditText (context); 또는 TextView 또는 whtvr ..) mView.findViewById') 뷰를 추가 할 위치입니다. 간단히'yourParentView.addView (새 뷰)를 호출하십시오; '. 그게 다야. –