【问题标题】:I can't append values to a string when an ImageView is clicked单击 ImageView 时,我无法将值附加到字符串
【发布时间】:2015-11-01 20:11:25
【问题描述】:

我正在尝试创建一个字符串,其内容取决于在活动中单击的图像。

因此,如果单击 Image1,则将给定的单词附加到字符串中,如果单击 Image2,则将第二个不同的单词附加到字符串中,依此类推。

然后,一旦单击所有(或部分)ImageView,我想使用这个字符串作为要通过 SHARE INTENT 发送的文本。

到目前为止一切顺利:已识别所有图像,按钮也是如此,附加方法工作正常,但是当我尝试通过电子邮件或短信共享字符串时,文本绝对是空的。

知道为什么吗?

这是我的 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"));
        }
    });
    }
}

如您所见,要共享的文本是字符串“result”,它应该由附加到它的文本组成。

在我的布局中,我只有两个带按钮的 ImageView。 (可点击=真)

<?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>

有什么想法吗??

【问题讨论】:

  • 你昨天问了同样的question,为什么又问了?!
  • 嗨 Mohamed 感谢您的提问!昨天我从 Saeed 那里得到了一半的答案,也就是将预设文本附加到字符串中。在您的情况下,它与 TextView 内容一起使用,这不是我的情况。此外,我需要单击和附加几个项目。现在,我设法只附加了一个预设值(而不是 TextView 的内容),但我无法在要共享的文本中获取字符串!

标签: android string share stringbuilder


【解决方案1】:

从StringBuffer 获取附加值。移动:

result = builder.toString();

在 Button 的 onClick 内。

出现问题是因为在调用 onCreate 方法时执行了 builder.toString() 行,并且在单击 ImageView 时附加了 StringBuffer 中的值。

【讨论】:

  • 你是个天才,谢谢!最后一个问题:当我点击图片时,我希望出现一个勾号,有什么想法吗?
  • @MDR:如果使用自定义复选框,为什么不更改 ImageView 点击或好
  • 嘀嘀嘀咕,你会从.xml布局还是activity.class?
  • @MDR:不是我你也能做到
  • 好的,最后一个问题。在第二次单击时,我希望删除 OnClick:这可行吗? public void onSecondClick(View v) { imageview2.setOnClickListener.cancel; } });
猜你喜欢
  • 2018-07-16
  • 2014-07-05
  • 2020-11-18
  • 1970-01-01
  • 2013-01-09
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多