Android 单击按钮后更改字母
在EclipseAndroid中单击按钮后,我想更改我的Android 单击按钮后更改字母,android,eclipse,Android,Eclipse,在EclipseAndroid中单击按钮后,我想更改我的TextView的第一个字母。我已经可以更改整个文本视图,但我只想更改第一个字母。它是如何工作的 XML: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent
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>
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
{}}}
像这样的东西可能有用
@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
的方法,请查看类似的方法
@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方法的实现:
@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());
}
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());
}
在您的
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)
String text=tw.getText().toString();
int n=3;// 在您的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)
String text=tw.getText().toString();
int n=3;// 试试这个:
String text = tw.getText().toString();
String substring = text.substring(1, text.length());
tw.setText(substring);
试试这个:
String text = tw.getText().toString();
String substring = text.substring(1, text.length());
tw.setText(substring);
我不知道这个解决方案。事实上,这很好。谢谢;)如果我帮了你,请接受这个答案。我没有问这个问题,因此不能接受你的答案,但我投了赞成票。我不知道这个解决方案。事实上,这很好。谢谢;)如果我帮了你,请接受这个答案。我没有问这个问题,因此不能接受你的答案,但我投了赞成票。只是看了一下答案。使用replace(0,1,“NEW_CHAR”)作为答案不是更好吗是的;)这是更好的,但我是匆忙写这篇文章。我会换的。谢谢你的建议!我只是看了看报纸。使用replace(0,1,“NEW_CHAR”)作为答案不是更好吗是的;)这是更好的,但我是匆忙写这篇文章。我会换的。谢谢你的建议!这只会删除第一个字母?!这只会删除第一个字母?!非常感谢你!你知道如何直接给C上色吗?@user334132这是不可能的,但是你可以找到一个解决方案来达到你的目的,在这个解决方案中,你应该把3个TextView
s放在一行,然后你可以改变中间一个的颜色。但是,如果你觉得我的答案很有帮助,请点击勾号图标接受我的答案。非常感谢!你知道如何直接给C上色吗?@user334132这是不可能的,但是你可以找到一个解决方案来达到你的目的,在这个解决方案中,你应该把3个TextView
s放在一行,然后你可以改变中间一个的颜色。但是,如果您觉得我的答案有帮助,请点击勾号图标接受