简体   繁体   English

Android Studio 按钮清除 TextView 文本

[英]Android Studio Button to clear TextView text

I want to make a button to clear the TextView text.我想制作一个按钮来清除 TextView 文本。 Im using Android Studio.我正在使用 Android Studio。

ImageButton ImageButton3;
TextView TextView26;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageButton3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            TextView.text="";
        }
    });

You have to instantiate the text view: 您必须实例化文本视图:

ImageButton ImageButton3;
TextView TextView26;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView26 = (TextView) findViewById(R.id.<your text view id>);

    ImageButton3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            TextView26.setText=("");
        }
    });

You forgot to instantiate your views for ImageButton and TextView. 您忘记为ImageButton和TextView实例化视图。

ImageButton ImageButton3;
TextView TextView26;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageButton3 = (ImageButton) findViewById(R.id.myImgBtnId);
    TextView26 = (TextView) findViewById(R.id.myTvId);

    ImageButton3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            TextView26.text="";
        }
    });

I would recommend you to name your variables in camel case. 我建议您以驼峰式命名您的变量。
Also if you can choose a more meaningful name it will be better. 同样,如果您可以选择一个更有意义的名称,它将更好。

You gotta find BOTH your views before you can manipulate them in your code. 您必须先找到视图,然后才能在代码中进行操作。

ImageButton imageButton3; //name to clearTextButton?
TextView textView26; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imageButton3 = (ImageButton) findViewById(R.id.imgButtonId); //replace it with your true id in your xml
    textView26 = (TextView) findViewById(R.id.textViewId);

    imageButton3.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        textView26.setText("");
    }
});

XML is file code XML 是文件代码

 <com.google.android.material.textview.MaterialTextView
    android:id="@+id/tx_demo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Demo World!" />

<com.google.android.material.button.MaterialButton
    android:id="@+id/btnClear"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Clear text Button" />

Java is file code Java为文件代码

    txDemo = findViewById(R.id.tx_demo);
    btnClear = findViewById(R.id.btnClear);
    btnClear.setOnClickListener(v -> {
        txDemo.setText("");
    });

OR或者

    txDemo = findViewById(R.id.tx_demo)
    txDemo = findViewById(R.id.tx_demo)
    btnClear = findViewById<MaterialButton>(R.id.btnClear)
    btnClear.setOnClickListener(View.OnClickListener { v: View? ->
        txDemo!!.text = ""
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM