2011年7月31日星期日

實現編輯文字(EditText)的清除按鈕

實例:
實現編輯文字(EditText)的清除按鈕

package com.AndroidEditText;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AndroidEditTextActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final EditText input = (EditText)findViewById(R.id.input);
Button buttonClear = (Button)findViewById(R.id.clear);
buttonClear.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
input.setText("");
}});

}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="CLEAR"
android:layout_alignParentRight="true"/>
<EditText
android:id="@+id/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="請輸入"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/clear"/>

</RelativeLayout>
</LinearLayout>


沒有留言:

發佈留言