按下按钮时EditText到TextView

时间:2012-07-14 20:03:50

标签: android sdk

我怎样才能使它使用edittext中的任何内容,就像文本视图一样,如果我输入“hello”按下按钮,它将textview设置为“hello”然后当你再次按下它时它会删除它?

  package com.purelymean.earnings;
  import org.w3c.dom.Text;

  import android.app.Activity;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.TextView;

    public class Main extends Activity{
/**Called when activity is first created. */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    Button b = (Button) findViewById(R.id.button);
    Text et = (Text) findViewById(R.id.editText1);
    TextView tx = (TextView) findViewById(R.id.textView1);


    b.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View v) {





        }
    });

}
}

4 个答案:

答案 0 :(得分:2)

另一种方法(添加到La bla bla):要在TextView中显示editText,首先要创建一个单击按钮时发生的方法:

public void displayEditText(View view) { }

在您的按钮的xml中添加以下内容:

android:onClick="displayEditText"

要获取EditText值,请将其放在displayEditText

String editTextValue = et.getText().toString; 要显示此信息,请执行以下操作:

tx.setText(editTextValue);

最终解决方案:

public void displayEditText(View view) {        
    if (tx.getText().toString().equals("")) {
         String editTextValue =   et.getText().toString;
           tx.setText(editTextValue);
    } else {
         tx.setText("");
         tv.setText("");
    }
}

答案 1 :(得分:1)

好吧,如果你读过如何使用它,你可以自己想出来,但无论如何:

package com.purelymean.earnings;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;


private boolean isErase = false;
private EditText et;
private TextView tx;

public class Main extends Activity{
/**Called when activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button);
et = (EditText) findViewById(R.id.editText1);
tx = (TextView) findViewById(R.id.textView1);


b.setOnClickListener(new OnClickListener() {


    @Override
    public void onClick(View v) {

        if(isErase) {
            tx.setText(et.getText().toString());
        } else {
            tx.setText("");
        }
        isErase = !isErase;
    }
});

}

您只需从EditText获取文本,并将其设置为TextView的文本。比你更改布尔变量的状态,所以它会在下次点击时删除。

另请注意,我从Text对象更改为EditText 代码未经测试,可能包含拼写错误和内容。

答案 2 :(得分:0)

package com.purelymean.earnings;
import org.w3c.dom.Text;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Main extends Activity{
   // You have to save reference to views that will be used in onClick handler.
   EditText et;  
   TextView tx;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    Button b = (Button) findViewById(R.id.button);
    et = (EditText) findViewById(R.id.editText1);
    tx = (TextView) findViewById(R.id.textView1);

    b.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
        // If TextView is empty - copy string from EditText
        if (tx.getText().toString().equals("")) {
             tx.setText(et.getText().toString());
        } else { // Otherwise, clear the TextView.
             tx.setText("");
        }
    }
});

}

答案 3 :(得分:0)

1)在此函数中传递textview id

       private void EditToText(TextView tv) {

        tv.setCursorVisible(false);
        tv.setFocusableInTouchMode(false);
        tv.setEnabled(false);

    }