Android - 如何清除 textView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12610574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:11:43  来源:igfitidea点击:

Android - How to clear textView?

androidtextview

提问by user1170168

I have created an android app in java using eclipse which creates a Sqlite database and lets the user add data to it. Users can also search for existing data. I used android cursor objects. How do I clear the text box after a query is completed ? For example, I type "value" to retrieve all records associated with that value. Now I want to perform another search. When I click on the text box, I would like the text box to be cleared for my next search.

我使用 eclipse 在 java 中创建了一个 android 应用程序,它创建了一个 Sqlite 数据库并让用户向其中添加数据。用户还可以搜索现有数据。我使用了android光标对象。查询完成后如何清除文本框?例如,我键入“value”来检索与该值关联的所有记录。现在我想执行另一次搜索。当我单击文本框时,我希望为下一次搜索清除文本框。

This is part of my code for inserting and retrieving data :

这是我插入和检索数据的代码的一部分:

db.execSQL("CREATE TABLE IF NOT EXISTS " + MY_DATABASE_TABLE + " (id INTEGER PRIMARY KEY AUTOINCREMENT, " + " name TEXT," + " number TEXT);");

Cursor cur = db.rawQuery("INSERT INTO " + MY_DATABASE_TABLE + " (name, number)" + " VALUES ( \"" + newname + "\", \"" + newnumber + "\");", null);
cur.moveToFirst();
while (cur.isAfterLast() == false) 
{
    result.append("\n" + "Id: " + cur.getString(0) + " | " + "Name: " + cur.getString(1) + " | " + "Phone Number: " + cur.getString(2) + "\n");

    cur.moveToNext();
}
cur.close(); 

Cursor c = db.rawQuery("SELECT * FROM " + MY_DATABASE_TABLE, new String [] {}  );

c.moveToFirst();

while (c.isAfterLast() == false) {
    result.append("\n" + "Id: " + c.getString(0) + " | " + "Name: " + c.getString(1) + " | " + "Phone Number: " + c.getString(2) + "\n");

    c.moveToNext();
}
c.close();

Thanks.

谢谢。

回答by Dave

First you probably don't want to clear your database to just clear a textView. So the relevant code would be inside your activity.

首先,您可能不想清除数据库来清除 textView。因此相关代码将在您的活动中。

So if you are using a textView you would do something like this in code

所以如果你使用的是 textView 你会在代码中做这样的事情

// Global variable

// 全局变量

    TextView yourTexView;

// In your activities on Create

// 在你的 Create 活动中

    yourTexView  = (TextView) findViewById(R.id.yourTexView );
    yourTexView .setOnClickListener(yourClickListener)

// Somewhere In the activities class

// 在活动类的某个地方

/**
 * Tour Click Handler - This will clear the textview whenever someone clicks it. 
 */
private OnClickListener yourClickListener= new OnClickListener() {
    public void onClick(View v) {
        yourTexView.setText("");
    }
};

// In your xml file - change as you need

// 在您的 xml 文件中 - 根据需要更改

   <TextView
        android:id="@+id/yourTexView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

Hopefully this helps, Cheers

希望这会有所帮助,干杯

回答by realkstrawn93

Either setText("")or setText(null)— either one of those would work. I use setText(null)for readability reasons.

无论是setText("")setText(null)-那些任何一个会工作。我setText(null)出于可读性原因使用。