如何在string.replace()方法中设置值?

时间:2017-01-01 09:48:34

标签: android str-replace setvalue getvalue

我有2个edittext。我想获得值edittext1和edittext 2并将其设置为string.replace mehod

像这样;  string.replace(“set here1st edittex value”,此处设置第二个edittext值“)

怎么可能呢?

先谢谢!!

 public class MainActivity extends Activity {
EditText et1,et2,et3,etoutput,et5;
TextView tv;
Button btn;

String change; 
//String oldtext=et1.getText().toString();
//String newtext=et2.getText().toString();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et1=(EditText) findViewById(R.id.editText1);
    et2=(EditText) findViewById(R.id.editText2);
    et3=(EditText) findViewById(R.id.editText3);
    et5=(EditText) findViewById(R.id.editText5);
    etoutput=(EditText) findViewById(R.id.editText4);
    btn=(Button) findViewById(R.id.button1);


btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
        String s1=et1.getText().toString();
        String s2=et2.getText().toString();

    ///  say error in oldString
       /// String newString=oldString.replace(s1,s2);


        }
    });
  //////Log.v("new vluew", newString);

  ////////////// for check that value is update ?? or not update/////////////////
 et3.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {
   }

   public void beforeTextChanged(CharSequence s, int start, 
     int count, int after) {
   }


public void onTextChanged(CharSequence s, int start, 
     int before, int count) {

       MainActivity.this.cha(s);
       MainActivity.this.change2(s);
   }
  });


}
@SuppressWarnings("unused")
private String cha(CharSequence arg) {
    String change = "";
    String s1=et1.getText().toString();
    String s2=et2.getText().toString();
    String change1=change.replace(s1,s2);
    this.etoutput.setText(change1);
    System.out.println(change1);
     return change;
     }
 @SuppressWarnings("unused")
private String change2(CharSequence arg) {
    String change = "";
    change= arg.toString().replace("", "" );
    this.et5.setText(change);
    System.out.println(change);
     return change;
     }



}

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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.replce.MainActivity" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:ems="10"
    android:inputType="textMultiLine" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_below="@+id/editText1"
    android:ems="10"
    android:inputType="textMultiLine" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText2"
    android:layout_below="@+id/editText2"
    android:layout_marginLeft="61dp"
    android:layout_marginTop="100dp"
    android:text="Button" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText2"
    android:ems="10"
    android:inputType="textMultiLine" />

<EditText
    android:id="@+id/editText4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText3"
    android:layout_below="@+id/button1"
    android:layout_marginTop="14dp"
    android:ems="10"
    android:inputType="textMultiLine" />

<EditText
    android:id="@+id/editText5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText4"
    android:layout_marginTop="37dp"
    android:ems="10"
    android:inputType="textMultiLine" />

 </RelativeLayout>

由于

1 个答案:

答案 0 :(得分:1)

是的,你可以。执行以下步骤:

  1. 在您的活动类中获取控件(edittexts,button)。
  2. 在按钮上添加点击监听器。
  3. 在侦听器方法中执行交换。
  4. 代码:

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        EditText edittext1 = (EditText)findViewById(R.id.edittext1);
        EditText edittext2 = (EditText)findViewById(R.id.edittext2);
        Button mButton = (Button)findViewById(R.id.button);
    
        mButton.setOnClickListener(
        new View.OnClickListener()
        {
            public void onClick(View view)
            { 
                String s1 = edittext1.getText().toString()
                String s2 = edittext2.getText().toString()
                String newString = oldString.replace(s1, s2);
    
                Log.v("new value", newString);
            }
        });
    }