我有一個非常基本的示例,允許用戶在微調框中選擇一個項目,並根據所選項目在圖像視圖中顯示對應圖像。在點擊按鈕時更改文本視圖的一個字
此外,我有一個默認說「選擇一個項目」的文本視圖,當選擇項目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
你真的只需要保存微調器的名稱和位置,並根據該代碼進行字符串替換。 –