开发者

action to an imageButton

开发者 https://www.devze.com 2023-02-10 04:16 出处:网络
public EditText text; public TextView text1; @Override public void onCreate(Bundle savedInstanceState) {
public EditText text;
public TextView text1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

}
        public void act(View v) {
            text = (EditText) findViewById(R.id.widget30);
            text1 = (TextView) findViewById(R.id.textView1);
            text1.setText(text.getText());


        }

and this the code XML

     <EditText
android:id="@+id/widget30"
android:layout_width="260px"
android:layout_height="50px"
android:text="Gouvernorat"
android:textSize="18sp"
android:layout_x="31px"
android:layout_y="90px"
></EditText><ImageButton 
android:layout_width="wrap_content" 
android:src="@drawable/icon1" 
android:id="@+id/imageButton1" 
android:layout_height="wrap_content" 
android:layout_x="108dip" 
android:layout_y="360dip">
android:onClick="act"
</ImageButton><TextView 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:id="@+id/textView1" 
and开发者_如何学Pythonroid:text="TextView" 
android:layout_x="196dip" 
android:layout_y="382dip">
</TextView>

how can i field in the textview the content of EditText ?

Thank you


Your code looks incompelete:

So I am writing a sample code block to understand the whole logic:

private ImageButton button;
    private EditText et;
    private TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); // your layout file name

        button = (ImageButton) findViewById(R.id.id_of_image_button); // your image button
        et = (EditText) findViewById(R.id.id_of_edit_text); // your edit text  field
        tv = (TextView) findViewById(R.id.id_of_text_view); // your text view

        // click event on your button
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Do something with the value of the button
                // sets the value of the edit text field to the text view
                tv.setText(et.getText().toSting());
            }
        });
    }

Also you should write your XML layout file in a correct way.


Not sure if this is directly copied from your source code, but there's a typo in your xml. The onClick attribute is outside your ImageButton tag.

Other than that, you need to call toString on what you get back from EditText.getText. The getText method returns an object of type Editable and not the underlying string

Try this for your act method

    public void act(View v) {
        text = (EditText) findViewById(R.id.widget30);
        text1 = (TextView) findViewById(R.id.textView1);
        text1.setText(text.getText().toString());
0

精彩评论

暂无评论...
验证码 换一张
取 消