2012-06-26 25 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就可以了。

相關問題