Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

android - How to append text with OnClick method

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

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

1 Reply

0 votes
by (71.8m points)

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;


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...