2012-06-26 23 views
0

我从android数据库中获取数据。数据不过是一个字符串(str1)。现在得到这些数据后,我想将str2添加到str1中。添加完后,我想把 作为str2的onclick动作。我可以在android中做到这一点?如果是的话,我该如何实现这一任务?请帮我完成这个任务......如何将一个字符串连接到其他字符并将onclick动作写入其中?

将非常感激....

+0

你试过了吗?请在这里分享一些代码 –

+0

你能解释一下你的意思吗?“添加完后,我想为该str2添加onclick动作”? str2是一个字符串。 onClick用于查看。你的意思是onClick显示str2的内容的TextView? – Abhilash

+0

在html中,我们保持超链接na..str2不过是我想要放一个名为“图像”的字。如果点击图片文字,弹出的图片应该显示...这可能吗? – user1448108

回答

0

我假设你正在尝试添加设置StringTextView的就加onClick行动。这是一个例子。在您的活动中创建此方法。

private void setTextView(TextView t, String str1, String str2) { 
    // Add str2 to str1 (append) 
    StringBuilder s = new StringBuilder(str1); 
    s.append(str2); 

    //Set text to textview 
    t.setText(s.toString()); 

    //set onclicklistener. 
    t.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // do whatever you want here. If you want 
      // to show an image make an AlertDialog. 
      // I have given link below. 

     } 
    }); 
} 

修改main.xml中包含一个TextView:

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

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

</LinearLayout> 

现在,在您Activty的onCreate补充一点:

TextView t = (TextView) findViewById(R.id.string); 
setTextView(t, str1, str2); 

AlertDialog与图像:http://eventuallyconsistent.net/2011/07/28/create-an-android-dialog-with-an-image/

0

Wriet验证码XML格式:

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Click Me" 
    android:onClick="onClick"     
    android:clickable="true" /> 

在你的活动,写:

TextView textActivity = (TextView)findViewById(R.id.textView1); 
    String2 = String1 + "Some text"; 
    textActivity.setText(String2); 
    textActivity.setOnClickListener(this); 
    ..... 
    ..... 

    public void onClick(View v) { 
     ... 
     } 
0

我不知道你的问题,但我的解决方案可以帮助你。

创建一个TextView &集str2作为TextView文本。

myView.setText(str2); 

现在申请onClickListener就可以了。

相关问题