Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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("
");
        builder.append(value3).append("
");
        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("
");
        }
    }

}

@Override
public void onClick(View V){
    Toast.makeText(getApplicationContext(),getApplicationContext().getResources().getString(Integer.parseInt(result)), Toast.LENGTH_LONG).show();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
281 views
Welcome To Ask or Share your Answers For Others

1 Answer

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;


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...