【问题标题】:how to show text on button press, hide on release and show another text in the same textview on pressing another button如何在按下按钮时显示文本,在释放时隐藏并在按下另一个按钮时在同一文本视图中显示另一个文本
【发布时间】:2016-04-14 15:41:29
【问题描述】:

我在我的应用程序中的某个地方感到震惊。我的应用程序上有 50 个按钮。只要按下按钮,我想显示文本并在释放时隐藏它。此外,在按下另一个按钮时,文本应在释放时更改并隐藏,依此类推。文本的位置保持不变。 (另外,我们可以使用 X 和 Y 坐标来设置文本的位置吗?)

下面是我的 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

  <TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:text="@string/NUMBERS"
    android:textColor="#ecaa00"
    android:textSize="28sp" 
    android:padding="10dip"/>
  <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:padding="3dip">


<Button 
    android:id="@+id/one"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:text="@string/ONE" />

<Button 
    android:id="@+id/two"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:text="@string/TWO" />

<Button 
    android:id="@+id/three"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:text="@string/THREE" />

<Button 
    android:id="@+id/four"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:text="@string/FOUR" />

<Button 
    android:id="@+id/five"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:text="@string/FIVE" />
</LinearLayout>
</LinearLayout>

PS:有很多按钮,为每个按钮设置可见属性并在释放时隐藏它们需要大量代码行。有没有更好的方法来实现同样的目标?例如引用按钮,按下时显示文本并在释放时隐藏。

【问题讨论】:

标签: android button text show-hide


【解决方案1】:

好吧,下面是你的textView

TextView textView = (TextView) findViewById(R.id.textView);

您可以像这样实现您的要求。

textView .setOnTouchListener(show_text);

show_text 的实现如下。

在这里,我正在更改给定 textView 的文本类型,但如果您愿意,您可以分别在 Touch_Down/Touch_Up 上显示 View 可见/不可见

private View.OnTouchListener show_text = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (v.getId() == R.id.one) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                make_Toast("you can implement your own action here when the button is pressed");
                textView.setTransformationMethod(null);
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                textView.setTransformationMethod(new PasswordTransformationMethod());
            }
            return true;
        }
        return false;
    }
};

【讨论】:

  • 感谢@Pankaj。我使用了您的 MotionEvent.ACTION_DOWN 方法,并按照您的建议将其可见性设置为可见和不可见。但是,此方法要求我将代码添加到布局中的每个按钮。有没有办法将按钮命名为 button1、button2、button3 并通过递增为 button+“variable”来循环它们。并将文本显示为if button1 text=one 等等。简而言之,我想问的是,当以最少的代码行按下和释放按钮时,是否有一种动态方式让所有按钮显示和隐藏文本?
  • @amit,是的,您当然可以这样做,创建一个按钮数组,初始化该数组中的所有按钮,然后对该数组运行 for 循环并应用 show_text 的实现b> 侦听器,您的工作将完成....您必须在侦听器中更改的唯一部分是应用切换块来检查单击了哪个按钮,然后您就完成了:)
  • bt[0] = (按钮) findViewById(R.id.bt0);在 xml 中,我必须将每个按钮定义为 bt0、bt1 .. 等等。我说得对吗@Pankaj
猜你喜欢
  • 1970-01-01
  • 2016-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多