Android 点击文本视图以显示文本的代码
我正在开发一个android应用程序,这是我的第一个应用程序开发 根据我的要求,我需要代码来实现UI,如下图所示 跳转的图像解释了我的要求。下图是应用程序UI视图。谢谢 我使用了下面的代码,但它不工作。有人能找出毛病吗Android 点击文本视图以显示文本的代码,android,Android,我正在开发一个android应用程序,这是我的第一个应用程序开发 根据我的要求,我需要代码来实现UI,如下图所示 跳转的图像解释了我的要求。下图是应用程序UI视图。谢谢 我使用了下面的代码,但它不工作。有人能找出毛病吗 - DrinkActivity.java TextView t ; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
- DrinkActivity.java
TextView t ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drink);
t = (TextView)findViewById(R.id.TextView01);
t.setOnClickListener((android.view.View.OnClickListener) this);
}
public void onClick(View arg0) {
t.setText("My text on click");
}
- activity_drink.xml
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
<ListView
android:id="@+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</ListView>
<LinearLayout
android:id="@+id/LinearLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
<TextView android:text="This is my first text"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:textStyle="bold"
android:textSize="28sp"
android:editable="true"
android:clickable="true"
android:layout_height="wrap_content"
android:onClick="onClick">
</TextView>
-DrinkActivity.java
文本视图t;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_-Diff);
t=(TextView)findViewById(R.id.TextView01);
t、 setOnClickListener((android.view.view.OnClickListener)this);
}
公共void onClick(视图arg0){
t、 setText(“单击时的我的文本”);
}
-activity_drink.xml
您可以通过设计两个片段来实现此行为:
1。第一个将是一个简单的文本视图片段,它将仅显示选项卡文本
2.第二个片段将作为一个LinaerLayout垂直布局,将依次包含两个文本视图,第一个片段将与第一个片段中的文本视图相同,而第二个文本视图将包含选项卡的说明
然后,您只需单击第一个片段,然后使用以下代码将其替换为第二个片段:
if (!isTabOpen)
{
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(TabFragment)
.add(R.id.containerForFragments,TabAndDescriptionFragment)
.commit();
}
else
{
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(TabAndDescriptionFragment)
.add(R.id.containerForFragments,TabFragment)
.commit();
}
创建plusButtonFragment和userNewFragment之后。对于如此简单的东西来说,这太过分了。一个带有onClick实现的简单文本视图可以将ScrollView的可见性从View.INVISIBLE更改为View.VISIBLE就够了。也许你是对的。但是如果他想重新复制这个并创建一个包含这些内容的滚动视图呢?我认为在一个scrollview中放置许多scrollview会有问题,通过这种方式他可以更加灵活。我在问题中进行了修改。。我已经添加了我使用过的代码..这不是working@SamitKumarPatra,我不能,因为我没有。我得和你一样写。我们在这里指导您并回答您可能遇到的任何问题,但不是为您编写代码。您可以在同一文本视图上单击并设置文本。这是故意的吗