如何使用 OnClick 方法附加文本

How to append text with OnClick method

我想创建一个字符串,其组件在按下 ImageView 时附加。

我有一个名为 imageView 的 ImageView 并在单击时将其命名为 ImageView 我将一个值附加到一个字符串。 最后,当单击一个按钮时,我希望该字符串与在上次覆盖中调用的 Toast 一起显示。 因此,我已经定义了字符串(命名结果)、按钮和图像,但我仍然遇到很多错误,最值得注意的是我在附加字符串时不记得 "value1"。

有什么想法吗?

Button button;
Button Click = (Button) findViewById(R.id.button);
String result;
ImageView Imageview = (ImageView) findViewById(R.id.imageView);



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

(...)

public class Program {
    public void main(String[] args) {

        final int value1 = 300;
        double value2 = 3.14;
        short value3 = 5;
        char value4 = 'A';

        // Create StringBuilder and add four values to it.
        final StringBuilder builder = new StringBuilder();
        builder.append(value2).append("\n");
        builder.append(value3).append("\n");
        builder.append(value4);

        // Display results.
        String result = builder.toString();
        System.out.println(result);


        public void onClick(View v) {

        Imageview.setOnClickListener(new View.OnClickListener() {
        builder.append(value1).append("\n");
        }
    }

}

@Override
public void onClick(View V){
    Toast.makeText(getApplicationContext(),getApplicationContext().getResources().getString(Integer.parseInt(result)), Toast.LENGTH_LONG).show();
}

TextView 然后 append(); :

XML中:

<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    android:text="" />

Java中:

public void onClick(View v) {
    Imageview.setOnClickListener(new View.OnClickListener() {
        TextView tv = (TextView) findViewById(R.id.textView);
        tv.setMovementMethod(new ScrollingMovementMethod());
        tv.append(value1 + "\n");
    }
}

您可以在单击按钮时附加这样的字符串

String stringName = "";
Imageview.setOnClickListener(new View.OnClickListener() {
    strigName = stringName + "New appended data";
}

button.setOnClickListener(new View.OnClickListener() {
    Toast.makeText(MainActivity.this, stringName, Toast.LENGTH_SHORT).show();
}

这一行的第一个错误

 ImageView Imageview = (ImageView) findViewById(R.id.imageView);

你不能在你的 class 块中使用 findById 你必须在像 onCreate 这样的函数中使用它所以改变你的代码......你的代码必须 像这样

    ImageView imageView;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        imageView= (ImageView) findViewById(R.id.imageView);
        textView=(TextView) findViewById(R.id.tv_show);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String test=textView.getText().toString();
                textView.setText(test);

            }
        });
    }

请记住,您必须将 textView 添加到 xml 文件并将此 ID 设置为 tv_show;

你的主要 Activity Java class 有这样的东西

public class MainActivity extends AppCompatActivity {

    Button button;
    Button click;
    String result ="";

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


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

//set the type
                shareIntent.setType("text/plain");

//add a subject
                shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                        "Insert Subject Here");

//build the body of the message to be shared
                String shareMessage = "Insert message body here.";

//add the message
                shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
                        shareMessage);

//start the chooser for sharing
                startActivity(Intent.createChooser(shareIntent, getResources().getString(R.string.FinalString)));
            }
        });
        // Create StringBuilder and add four values to it.
        final StringBuilder builder = new StringBuilder();
        builder.append(value2).append("\n");
        builder.append(value3).append("\n");
        builder.append(value4);

        ImageView imageview = (ImageView) findViewById(R.id.imageView);
        imageview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder.append(value1).append("\n");

            }
        });
        click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,builder.toString(),Toast.LENGTH_LONG).show();
            }
        });


    }




}

我把你的XMl文件改成这样更好

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="88dp"
        android:clickable="true"
        android:src="@android:drawable/ic_input_add" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />


</LinearLayout>

我测试一切正常

并对您的错误发表评论there