我有一个非常基本的示例,允许用户在微调框中选择一个项目,并根据所选项目在图像视图中显示对应图像。在点击按钮时更改文本视图的一个字
此外,我有一个默认说“选择一个项目”的文本视图,当选择项目1时,此文本视图显示“项目1的名称是无”,当选择项目2时,出现“项目的名称2不是“。
现在我有一个editext和一个按钮,我想在editext中输入一个名称,当我在editext中输入一个名称(例如John)并单击按钮时,我希望textview中的文本更改为“item1的名称是约翰”。我试着用下面的代码,但它不工作。你知道如何解决?
例如:如果我编写“john”并单击按钮而不是出现“item2的名称是john”出现“john item2的名称是none”。
活动的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>
字符串的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>
代码:
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()));
}
究竟你**没有工作意味着什么**?点击按钮时它会做什么? – kRiZ
如果我编写“john”并单击按钮而不是出现“item2的名称是john”出现“john item2的名称是none”。 – JonD
你真的只需要保存微调器的名称和位置,并根据该代码进行字符串替换。 –