How to append text with OnClick method

2019-09-18 03:10发布

问题:

I would like to create a string whose components are appended once an ImageView is pressed.

I have an ImageView called imageView and named ImageView on clicking which I am appending a value to a string. Finally when clicking on one button, I would like the string to show up with a Toast called out in the last Override. I have, therefore, defined the string (named result), the button and the image, but I still get many errors, most notably I can't recall the "value1" when appending the String.

Any ideas?

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();
}

回答1:

Your Main Activity Java class have somthing like this

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();
            }
        });


    }




}

i change Your XMl File To This iths better

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

I Test It every things Fine

And Show comment on Your Error there



回答2:

the frirst error for this line

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

you can't use findById in your class block you have to use it in a function like onCreate so change your code .... your code have to something like this

    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);

            }
        });
    }

just remember you have to add a textView to your xml file and set this id to tv_show;



回答3:

Make TextView then append(); :

in XML:

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

in 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");
    }
}


回答4:

You can append the string like this on button click

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();
}