如何在C#中添加一个Clear Text Field按钮到我的Android应用程序?

时间:2013-01-22 20:14:27

标签: c# android xml xamarin.android

我非常喜欢编程..但我正在尝试使用monodroid开发一个小型Android应用程序来存储联系人并从地址簿中调用联系人。

如果这很简单,请原谅我,但我有一个文本字段和一个按钮,这两个文件都在我的资源XML文件中,我希望能够通过单击按钮清除文本框中的文本被称为“清除”..所有的帮助非常感谢,因为我期待着学习更多。

2 个答案:

答案 0 :(得分:0)

怎么样?

  Button ClearText=(Button) findViewById(R.id.ClearText);
  ClearText.setOnClickListener(new View.OnClickListener(){
  public void onClick(View v){
    EditText textedit=(EditText) findViewById(R.id.textedit);
    textedit.setText("");
  }
 });
必须将

clear注册为布局文件中按钮的onclick处理程序,如此

   <ImageButton android:id="@+id/ClearText"
    android:text="@string/ClearText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    **android:onClick="clear"**          
    android:src="@drawable/clear"
    />

答案 1 :(得分:0)

让我们假设你有以下布局,我假设它与你已经有的相似:

<?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:id="@+id/TextField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/Clear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Clear"/>
</LinearLayout>

然后在您的活动代码中,您可以执行以下操作:

var clear = FindViewById<Button>(Resource.Id.Clear);
var textField = FindViewById<TextView>(Resource.Id.TextField);

clear.Click += (src, args) =>
               {
                   textField.Text = "";
               };