简体   繁体   English

单击 ImageView 时,我无法将值附加到字符串

[英]I can't append values to a string when an ImageView is clicked

I am trying to create a string whose content is dependent on the images clicked in an activity.我正在尝试创建一个字符串,其内容取决于在活动中单击的图像。

So, if Image1 is clicked, a given word is appended to the string, if Image2 is clicked, a second different word is appended to the string and so on.因此,如果单击 Image1,则将给定的单词附加到字符串,如果单击 Image2,则将第二个不同的单词附加到字符串,依此类推。

Then, once all (or some) of the ImageViews are clicked, I would like to use trhis string as text to be sent through the SHARE INTENT.然后,一旦单击所有(或部分)ImageViews,我想使用 trhis 字符串作为要通过 SHARE INTENT 发送的文本。

So far so good: all images have been identified, same for the button, the append method is working fine, but when I try to share the string via e-mail or SMS, the text is absolutely empty.到目前为止一切顺利:所有图像都已识别,按钮也是如此,append 方法工作正常,但是当我尝试通过电子邮件或 SMS 共享字符串时,文本绝对是空的。

Any idea why?知道为什么吗?

This is my MainActivity这是我的主要活动

public class MainActivity extends AppCompatActivity {

Button button;
String result ="";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.button);
    final int value1 = 300;
    final double value2 = 3.14;
    final short value3 = 5;
    final char value4 = 'A';


    // Create StringBuilder and add four values to it.
    final StringBuilder builder = new StringBuilder();


    ImageView imageview = (ImageView) findViewById(R.id.imageView);
    imageview.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {
            builder.append(value1).append("\n");
        }
    });

    ImageView imageview2 = (ImageView) findViewById(R.id.imageView2);
    imageview2.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {
            builder.append(value2).append("\n");
        }
    });

    result = builder.toString();


    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //create the send intent
            Intent shareIntent =
                    new Intent(Intent.ACTION_SEND);

            shareIntent.setType("text/plain");

            shareIntent.putExtra(Intent.EXTRA_SUBJECT,
                    "Insert Subject Here");

            String shareMessage = result;

            shareIntent.putExtra(Intent.EXTRA_TEXT,
                    shareMessage);

            startActivity(Intent.createChooser(shareIntent, "Share via"));
        }
    });
    }
}

as you can see, the text to be shared is the string "result" which should be composed of the text appended to it.如您所见,要共享的文本是字符串“result”,它应该由附加的文本组成。

In my layout I have simply two ImageViews with a button.在我的布局中,我只有两个带有按钮的 ImageView。 (clickable=true) (可点击=真)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="88dp"
    android:clickable="true"
    android:background="@mipmap/ic_launcher"
    android:contextClickable="true" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView2"
    android:clickable="true"
    android:background="@mipmap/ic_launcher"
    android:layout_alignTop="@+id/imageView"
    android:layout_alignRight="@+id/button"
    android:layout_alignEnd="@+id/button"
    android:contextClickable="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Share"
    android:id="@+id/button"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:clickable="true"
    android:contextClickable="true" />


</RelativeLayout>

Any idea??任何的想法??

To get appended value from StringBuffer .StringBuffer获取附加值。 move :移动 :

result = builder.toString();

inside onClick of Button.在按钮的onClick里面。

Problem is occurring because builder.toString() line is executed when onCreate method is called and values in StringBuffer is appending on click of ImageView's.出现问题是因为在调用onCreate方法时会执行builder.toString()行,并且在单击 ImageView 时附加 StringBuffer 中的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM