单击按钮后更改一个字母

时间:2014-02-22 17:48:19

标签: android eclipse

点击Eclipse Android中的按钮后,我想更改TextView的第一个字母。我已经可以更改整个TextView,但我只想更改第一个字母。它是如何工作的?

XML:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/BtnKlick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="209dp"
        android:text="Button" />

    <TextView
        android:id="@+id/Text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:text="Eclipse"
        android:textSize="70sp" />

</RelativeLayout>

JAVA:

package com.example.testapp;

import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

    public Button btn;
    public TextView tw;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn=(Button)findViewById(R.id.BtnKlick);
        tw=(TextView)findViewById(R.id.Text);

        btn.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub


        {}}}

4 个答案:

答案 0 :(得分:4)

这样的事情可能有用。

@Override
public void onClick(View v) {
    // get the current string
    String toChange = tw.getText().toString();

    // get the part of the string you want to keep [everything after the first letter]
    String toKeep = toChange.subString(1, toChange.length);

    // put the letter you want before the part of the string you wanted to keep.
    toChange = "PUT_YOUR_NEW_LETTER_HERE".concat(toKeep);

    // finally set the new string in your TextView
    tw.setText(toChange);
}

如果你更清楚地解释你想要达到的目标,我可以帮助你。

当你询问如何在字符串中替换字符时,我建议你看看@ Rafik991的答案。您只需使用以下内容:

@Override
public void onClick(View v) {
    int index = 0; // the index at which you want to replace a char.
    StringBuilder stringBuilder = new StringBuilder(tw.getText);
    stringBuilder.replace(index, index + 1, "YOUR_NEW_CHAR_AS_A_STRING");
    tw.setText(stringBuilder.toString());
}

要了解StringBuilder的方法,请更好地了解official documentation,

答案 1 :(得分:2)

onChange方法的实现:

@Override
public void onClick(View v) {
        String cos = tw.getText();
        StringBuilder stringBuilder = new StringBuilder(cos);

        stringBuilder.replace(0, 1, 'c'); // c is your new character
   //or stringBuilder.deleteCharAt(0);
   //   stringBuilder.setCharAt(0,'c');

        tw.setText(stringBuilder.toString());

}

答案 2 :(得分:0)

onClick函数中尝试此操作:

String text = tw.getText().toString();
String newText = "C" + text.substring(1);
//                ^-- your new letter
tw.setText(newText)

<强>更新 如果你想改变另一个字母,它很容易!

String text = tw.getText().toString();
int n = 3; // <-- nth letter
String newText = text.substring(0, n) + "C" + text.substring(n + 1);
//                                       ^-- your new letter
tw.setText(newText)

答案 3 :(得分:0)

试试这个:

String text = tw.getText().toString();  
String substring = text.substring(1, text.length());
tw.setText(substring);