How to set value in string.replace() method?

537 views Asked by At

I have 2 edittext .and i want to get value edittext1 and edittext 2 and set it in string.replace mehod

Like this; string.replace("set here1st edittex value",set here 2nd edittext value")

How to possible it?

Thanks in advnce!!

 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>

Thanks

1

There are 1 answers

4
hasan On

Yes you can. with the following steps:

  1. Get you controls (edittexts, button) in you activity class.
  2. Add a click listener to your button.
  3. do the swap in the listener method.

Code:

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