Ich habe ein sehr einfaches Beispiel, das einem Benutzer ermöglicht, ein Element in einem Drehfeld auszuwählen und je nach ausgewähltem Element erscheint das entsprechende Bild in einer Bildansicht.ein Wort einer Textansicht auf Knopfdruck ändern
Auch ich habe eine Textansicht, die standardmäßig "Wählen Sie einen Artikel" und wenn der Artikel 1 ausgewählt ist diese Textansicht zeigt "Der Name des Artikels 1 ist keine" und wenn der Artikel 2 ausgewählt ist, wird angezeigt "Der Name des Artikels 2 ist keine ".
Und jetzt habe ich einen editext und eine Schaltfläche, und ich möchte einen Namen in der Editext eingeben und wenn ich einen Namen in der Editext (zum Beispiel John) eingeben und klicken Sie auf die Schaltfläche Ich möchte, dass der Text in der Textansicht ändert sich in "Der Name von item1 ist John". Ich versuche mit dem Code unten, aber es funktioniert nicht. Weißt du, wie man löst?
Beispiel: Wenn ich "John" schreibe und auf die Schaltfläche klicke Anstatt "Der Name von item2 ist John" erscheint "John Der Name von item2 ist keiner".
Aktivität 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"
tools:context="com.example.ricardorei.tpc.MainActivity">
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="48dp"
android:text="Button"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="166dp"
android:layout_marginRight="56dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginEnd="58dp"
android:onClick="addName"/>
<ImageView
android:id="@+id/image"
android:layout_width="0dp"
android:layout_height="220dp"
android:layout_marginBottom="16dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="200dp"
android:layout_height="30dp"
android:layout_marginLeft="32dp"
android:layout_marginRight="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:entries="@array/spinner_values"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBaseline_toBaselineOf="@+id/button3"
tools:layout_editor_absoluteX="32dp" />
<TextView
android:id="@+id/textView4"
android:layout_width="391dp"
android:layout_height="51dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.369"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.48" />
</android.support.constraint.ConstraintLayout>
String xml:
<resources>
<string name="app_name">tpc</string>
<string-array name="spinner_values">
<item>Select</item>
<item>item1</item>
<item>item2</item>
</string-array>
</resources>
Code:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner2);
String string = String.valueOf(spinner.getSelectedItem());
final ImageView image = (ImageView)findViewById(R.id.image);
final TextView txt = (TextView) findViewById(R.id.textView4);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
txt.setText("Select an item");
image.setImageResource(R.drawable.bg);
break;
case 1:
txt.setText("The name of item1 is none");
image.setImageResource(R.drawable.item1);
break;
case 2:
txt.setText("The name of item2 is none");
image.setImageResource(R.drawable.item2);
break;
default:
//Default image
//image.setImageResource(R.drawable.item2);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public void addName(View view) {
EditText editext = (EditText) findViewById(R.id.editText2);
TextView txt = (TextView) findViewById(R.id.textView4);
txt.setText(txt.getText().replace(5, 1, editext.getText()));
}
Was genau meinen Sie mit ** nicht funktioniert **? Was macht es, wenn Sie auf die Schaltfläche klicken? – kRiZ
Wenn ich "John" schreibe und auf die Schaltfläche klicke Anstatt "Der Name von item2 is john" erscheint "john Der Name von item2 is none". – JonD
Sie wirklich brauchen nur den Namen und die Position des Spinners speichern ab und ersetze ein String dieser –