【问题标题】:How do i add a Clear Text Field button to my android app in C#?如何在 C# 中向我的 android 应用程序添加一个清除文本字段按钮?
【发布时间】:2013-01-22 20:14:27
【问题描述】:

我是一个编程初学者。但我正在尝试开发一个使用 monodroid 的小型 android 应用程序来存储联系人并从通讯录中呼叫联系人。

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

【问题讨论】:

    标签: c# android xml xamarin.android


    【解决方案1】:

    假设您有以下布局,我假设它与您已有的类似:

    <?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 = "";
                   };
    

    【讨论】:

    • 大家好,感谢您的帮助,我仍然不确定将活动代码放在哪里 - 这是我所做的,但我遇到了这样的部署错误.. 我应该把它放在一个新的公共或私人课程..也感谢您的耐心,我知道这对您来说一定很痛苦!
    • 公共类 Activity1 : Activity { int count = 1; protected override void OnCreate(Bundle savedInstanceState){ base.OnCreate(savedInstanceState); SetContentView (Resource.Layout.Main); var clear = FindViewById
    • 我已经成功了,非常感谢 Greg.. 检查所有拼写后,我发现有些拼写与 xml 文件中指定的名称不匹配!再次感谢!
    【解决方案2】:

    这个怎么样?

      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"
        />
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    • 2013-07-09
    • 2019-07-07
    相关资源
    最近更新 更多