Ich würde Ihre Hilfe benötigen, um etwas in Android zu tun, im folgenden der Anwendungsfall. Ich habe einen benutzerdefinierten Dialog in Android erstellt, deren Layout ist: dialog_threshold.xmlHinzufügen von dynamischen Komponenten zu einem benutzerdefinierten Dialogfeld in 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>
Und in der Aktivität, wenn ich auf eine bestimmte Schaltfläche klicken, ich habe so etwas wie dies, um die Gewohnheit zu schaffen Dialog:
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);
}
});
Was will ich jetzt zu tun ist, dynamisch andere Komponenten hinzuzufügen (wie EditText oder Textview) zu dem oben Dialog, bevor es gezeigt. Wie kann ich es tun?
Vielen Dank im Voraus!
Hallo, danke für deine Antwort. Muss ich nur diese zwei Zeilen Code hinzufügen und es funktioniert? Könnten Sie mir bitte mehr Details geben? – Giulio
Ich habe es nicht verstanden Entschuldigung - könnten Sie mir bitte helfen? – Giulio
@Giulio .. ich meine ,, Sie müssen eine 'Ansicht' erstellen zuerst.eg:-' EditText edit = neu EditText (context); 'oder TextView oder whtvr .. Dann finden Sie die übergeordnete Ansichtsgruppe (zB: -' mView.findViewById'), wo Sie diese Ansicht hinzufügen möchten. dann ruf einfach 'yourParentView.addView (deine neue Ansicht); '. das ist es. –