如何在按下按钮时显示文本,在释放时隐藏并在按下另一个按钮时在同一文本视图中显示另一个文本
how to show text on button press, hide on release and show another text in the same textview on pressing another button
我对应用程序中的某个点感到震惊。我的应用程序上有 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:有很多按钮,为每个按钮设置可见属性并在发布时隐藏它们需要大量代码行。有没有更好的方法来实现同样的目标?例如引用按钮,按下时显示文本并在释放时隐藏。
好吧,下面是你的 textView
TextView textView = (TextView) findViewById(R.id.textView);
您可以像这样实现您的要求。
textView .setOnTouchListener(show_text);
show_text
的实现如下。
在这里,我正在更改给定 textView
的文本类型,但如果您愿意,可以分别在 Touch_Down/Touch_Up 上显示 View
visible/invisible
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;
}
};
我对应用程序中的某个点感到震惊。我的应用程序上有 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:有很多按钮,为每个按钮设置可见属性并在发布时隐藏它们需要大量代码行。有没有更好的方法来实现同样的目标?例如引用按钮,按下时显示文本并在释放时隐藏。
好吧,下面是你的 textView
TextView textView = (TextView) findViewById(R.id.textView);
您可以像这样实现您的要求。
textView .setOnTouchListener(show_text);
show_text
的实现如下。
在这里,我正在更改给定 textView
的文本类型,但如果您愿意,可以分别在 Touch_Down/Touch_Up 上显示 View
visible/invisible
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;
}
};