Android onClick函数对于edittext,如何调用edittext?

Android onClick函数对于edittext,如何调用edittext?,android,onclick,android-edittext,Android,Onclick,Android Edittext,我有一个可以填充的edittext表单,我想在文本的一次点击中清除它,所以我在main中放了一个进入我的clear函数的一次点击 然而,我不知道如何正确地针对我想要的编辑文本。我想清除其中两个 public void clear(View v) { @+id/toptext.setText(""); } 这是该特定文本的XML <TextView android:id="@+id/toptext" android:layout_width="match_parent" and

我有一个可以填充的edittext表单,我想在文本的一次点击中清除它,所以我在main中放了一个进入我的clear函数的一次点击

然而,我不知道如何正确地针对我想要的编辑文本。我想清除其中两个

public void clear(View v) {
    @+id/toptext.setText("");

} 
这是该特定文本的XML

<TextView
android:id="@+id/toptext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#303030"
android:text="@string/toptext"
android:textAppearance="?
android:attr/textAppearanceLarge"
android:textColor="#33B5E5"
android:textSize="50sp"
android:onClick="clear"  />

首先,通过调用findViewById获取对TextView的引用:

TextView textView = (TextView) findViewById(R.id.toptext);
然后用它来清除:

textView.setText("");

首先,通过调用findViewById获取对TextView的引用:

TextView textView = (TextView) findViewById(R.id.toptext);
然后用它来清除:

textView.setText("");
试试这个:

TextView tv = (TextView) findViewById(R.id.toptext);
tv.setText("Whatever you want!");
试试这个:

TextView tv = (TextView) findViewById(R.id.toptext);
tv.setText("Whatever you want!");

我认为不应该设置,而应该使用null作为

TextView textView = (TextView) findViewById(R.id.toptext);
textView.setText(null);

我认为不应该设置,而应该使用null作为

TextView textView = (TextView) findViewById(R.id.toptext);
textView.setText(null);

使用xml布局中EditText的id创建EditText的实例。然后在它们上使用setText并使其为空

public void clear(View v) {

    EditText et = (EditText)findViewById(R.id.theIdOfYourEditText);
    EditText et2 = (EditText)findViewById(R.id.theIdOfYourOtherEditText);
    et.setText("");
    et2.setText("");
} 
由于某种原因,编辑上述方法不起作用。这就是最终的解决方案:

// Put one of these in your onCreate method:

TextView tt = (TextView)findViewById(R.id.toptext);
tt.setOnTouchListener(new View.OnTouchListener() { 
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        EditText et = (EditText)findViewById(R.id.theIdOfYourEditText);
        EditText et2 = (EditText)findViewById(R.id.theIdOfYourOtherEditText);
        et.setText("");
        et2.setText("");

        return true;
    }
});

// or

TextView tt = (TextView)findViewById(R.id.toptext);
tt.setOnClickListener(new View.OnClickListener() { 
    @Override
    public void onClick(View v) {

        EditText et = (EditText)findViewById(R.id.theIdOfYourEditText);
        EditText et2 = (EditText)findViewById(R.id.theIdOfYourOtherEditText);
        et.setText("");
        et2.setText("");
    }
});

使用xml布局中EditText的id创建EditText的实例。然后在它们上使用setText并使其为空

public void clear(View v) {

    EditText et = (EditText)findViewById(R.id.theIdOfYourEditText);
    EditText et2 = (EditText)findViewById(R.id.theIdOfYourOtherEditText);
    et.setText("");
    et2.setText("");
} 
由于某种原因,编辑上述方法不起作用。这就是最终的解决方案:

// Put one of these in your onCreate method:

TextView tt = (TextView)findViewById(R.id.toptext);
tt.setOnTouchListener(new View.OnTouchListener() { 
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        EditText et = (EditText)findViewById(R.id.theIdOfYourEditText);
        EditText et2 = (EditText)findViewById(R.id.theIdOfYourOtherEditText);
        et.setText("");
        et2.setText("");

        return true;
    }
});

// or

TextView tt = (TextView)findViewById(R.id.toptext);
tt.setOnClickListener(new View.OnClickListener() { 
    @Override
    public void onClick(View v) {

        EditText et = (EditText)findViewById(R.id.theIdOfYourEditText);
        EditText et2 = (EditText)findViewById(R.id.theIdOfYourOtherEditText);
        et.setText("");
        et2.setText("");
    }
});
使用传递给onClick v的参数

假设您想清除文本视图中的文本,您的onClick已附加到该文本视图。传递到clear的视图是已单击的视图,因此您只需将其强制转换到适当的视图TextView,然后就可以正常使用它

因此,这将清除单击哪个TextView上的文本。

使用传递给onClick v的参数

假设您想清除文本视图中的文本,您的onClick已附加到该文本视图。传递到clear的视图是已单击的视图,因此您只需将其强制转换到适当的视图TextView,然后就可以正常使用它

因此,这将清除单击的文本视图上的文本。

您可以使用以下代码:

public void clear(View v) {
    if (v.getId() == R.id.toptext) {
        ((EditText) v).setText(null);
    }
}
同样,为另一个EditText添加另一个if条件。

您可以使用以下代码:

public void clear(View v) {
    if (v.getId() == R.id.toptext) {
        ((EditText) v).setText(null);
    }
}


同样,为另一个EditText添加另一个if条件。

我想清除其中两个条件。、,,第二个在哪里我想如果我能理解如何清除一个,我可以在清除功能中清除它下面的第二个。有任何答案对你有帮助吗?我正在使用你的答案。给我一点时间,我想清除其中两个,,第二个在哪里我想如果我能理解如何清除一个,我可以在清除功能中清除它下面的第二个。有任何答案对你有帮助吗?我正在使用你的答案。给我一点时间。这不是真的,是有效的,问题是在其他地方,如果我们使用null而不是,我们可以看到EditText的提示。您尝试将TextView设置为null而不是EditText,如果设置为clear,您可以在中看到提示EditText@Shayanpourvatan在que中,他要求使用EditText,而在xml中,他要求使用Textview。所以我使用了null,如果你设置为清除yoy可以看到提示,我想说,如果你在EditTExt中设置值为,你可以看到提示,不需要设置null,这不是真的,是有效的,问题是在其他地方,如果我们使用null而不是,我们可以看到EditTExt的提示你尝试将TextView设置为null而不是EditTExt,如果设置为“清除”,则可以在中看到提示EditText@Shayanpourvatan在que中,他要求使用EditText,而在xml中,他要求使用Textview。所以我使用了null,如果设置为clear yoy can see Hint,就不会让您出现在这里??我想说,如果您将EditTExt中的值设置为can see Hint,就不需要设置null,所以我用ID替换了代码,它会编译,但是单击textview并不能清除字段。@user3204660我使用您的textview测试了它,但它也不起作用。必须是文本视图的onClick方法。试试这个,告诉我使用onClick和onTouch监听器是否有效:当尝试使用onClick时,你是否尝试添加android:clickable=true?@codeMagic我在测试中使用了两个文本视图,另一个有效,但他使用的我复制和粘贴的一个无效。因此,我用ID替换了你的代码,并编译了它,但是单击textview并不能清除字段。@user3204660我使用您的textview测试了它,但它也不起作用。必须是文本视图的onClick方法。试试这个,告诉我使用onClick和onTouch监听器是否有效:在尝试使用onClick时,您是否尝试添加android:clickable=true?@codeMagic我在测试中使用了两个文本视图,另一个有效,但他使用的我复制和粘贴的一个没有。