2014-02-22 33 views
0

我想在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 


     {}}} 

回答

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) 
+0

如果我只想更改第三個字母,它是如何工作的? – user3341325

+0

@ user3341325我已更新我的答案,請參閱 –

+0

非常感謝!你知道如何爲C着色嗎? – user3341325

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更好的方法來看看的onChange方法的official documentation,

+0

快速反應:) – RMachnik

+1

只是看着[StringBuilder的文檔(http://docs.oracle.com/javase/1.5.0/docs/api/的java /郎/ StringBuilder.html)。用你的答案替換(0,1,「NEW_CHAR」)會更好嗎? :) – Endzeit

+0

是的;)這是更好,但我趕緊寫這篇文章。我會改變它。感謝您的建議! – RMachnik

2

實現:

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

} 
+0

不知道這個解決方案。實際上這很不錯。 – Endzeit

+0

謝謝;)如果我幫你,請接受這個答案。 – RMachnik

+0

我沒有問這個問題,因此不能接受你的答案,但我投了票。 – Endzeit

0

試試這個:

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

接受它只會刪除第一個字母?! – user3341325

+0

是的,它僅刪除每次點擊的第一個字符。 –