在按钮单击上更改textview的一个单词

时间:2017-09-26 22:18:46

标签: android onclick textview

我有一个非常基本的例子,允许用户在微调器中选择一个项目,根据所选的项目,它会在图像视图中显示相应的图像。

我还有一个默认情况下默认显示“选择项目”的文本视图,当选择项目1时,此文本视图显示“项目1的名称为无”,当选择项目2时,它显示为“项目2的名称”没有“。

现在我有一个editext和一个按钮,我想在editext中输入一个名字,当我在editext中输入一个名字(例如John)并点击按钮时我希望textview中的文字发生变化to“item1的名字是john”。我尝试使用下面的代码,但它无法正常工作。你知道如何解决吗?

示例:如果我写“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>

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>

代码:

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

    }

3 个答案:

答案 0 :(得分:1)

更好的方法是在字符串中使用将被用户选择的名称替换的标记。 这种方式具有额外的好处,即使用户更改微调器选择,用户选择的名称也将在TextView中替换。

首先,使用令牌占位符将字符串添加到strings.xml:

<resources>
    <string name="app_name">tpc</string>
    <string name="item_0">Select an item</string>
    <string name="item_1">The name of item1 is %1$s</string>
    <string name="item_2">The name of item2 is %1$s</string>
    <string-array name="spinner_values">
        <item>Select</item>
        <item>item1</item>
        <item>item2</item>
    </string-array>
</resources>

然后创建成员变量以跟踪名称和微调器位置:

  public class MainActivity extends AppCompatActivity {

    String mName = "none";
    int mSpinnerPos = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //.........
    }

修改您的微调器代码以更新当前选定的索引,并将名称替换为用户选择的名称:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 0:
                mSpinnerPos = 0;
                txt.setText(R.string.item_0);
                image.setImageResource(R.drawable.bg);
                break;
            case 1:
                mSpinnerPos = 1;
                txt.setText(String.format(getString(R.string.item_1), mName));
                image.setImageResource(R.drawable.item1);
                break;
            case 2:
                mSpinnerPos = 2;
                txt.setText(String.format(getString(R.string.item_2), mName));
                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);
    mName = editext.getText();

    TextView txt = (TextView) findViewById(R.id.textView4);
    switch (mSpinnerPos) {
      case 0:
        //nothing to do
        break;
      case 1:
        txt.setText(String.format(getString(R.string.item_1), mName));
        break;
      case 2:
        txt.setText(String.format(getString(R.string.item_2), mName));
        break;
    }
}

答案 1 :(得分:0)

你可以使用下面的代码作为一个非常简单的修复(直接替换&#34; none&#34;名称):

txt.setText(txt.getText().toString().replace("none", editext.getText().toString()));

注意:只有在单词&#34; none&#34;存在。如果您想用其他名称替换名称,这将不起作用。

我很惊讶您能够获得replace(int start, int end, CharSequence text)方法,因为它是Editable类的一部分。我的Android Studio抱怨说它不存在。

答案 2 :(得分:-1)

因此,不是拼接文本视图,而是制作单独的文本视图,并为这一个文本视图设置文本。例如,这是id&#34; name&#34;

的另一个textview
<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="firstString"
    app:layout_constraintRight_toLeftOf="@+id/name"
    android:layout_marginRight="7dp"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="@+id/editText2"
    app:layout_constraintHorizontal_bias="0.503"
    android:layout_marginTop="32dp"
    app:layout_constraintTop_toBottomOf="@+id/editText2" />

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="nameString"
    android:layout_marginTop="0dp"
    app:layout_constraintTop_toBottomOf="@+id/textView4"
    android:layout_marginBottom="0dp"
    app:layout_constraintBottom_toTopOf="@+id/textView4"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="@+id/button3" />

在您的代码中,我已将所有视图变量移到了类的顶部...您可以在onCreate中初始化它们,然后您可以在活动的任何位置引用它们。这意味着您只需要声明一次。

public class MainActivity extends AppCompatActivity {

TextView result;
 ImageView image;
 TextView txt;
EditText editText;

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

    result=(TextView)findViewById(R.id.name);




    image = (ImageView)findViewById(R.id.image);
    txt = (TextView) findViewById(R.id.textView4);
    editText = (EditText) findViewById(R.id.editText2);



    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");
                    break;
                case 1:
                    txt.setText("The name of item1 is none");
                    break;
                case 2:
                    txt.setText("The name of item2 is none");
                    break;
                default:
                    //Default image
                    //image.setImageResource(R.drawable.item2);
                    break;

            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

}

public void addName(View view) {
    result.setText(editText.getText());
}

}

请注意,在addName中,我只是使用编辑文本中的文本设置结果textview,而无需更改该微调器结果textview。

希望这有帮助!

<强> 修改

此外,如果您想使用相同的文本视图,而不是替换,您可以始终执行textview.append(&#34; yourString&#34;)。这可能更简单。