简体   繁体   English

处理Android中的多个按钮

[英]handle multiple buttons in android

In a table layout i have few text views and buttons for each row. 在表格布局中,每行都有很少的文本视图和按钮。 Now when i click a particular button only that text view value related to that particular button must be passed to next intent.How can i achieve this ? 现在,当我单击某个特定按钮时,仅必须将与该特定按钮相关的文本视图值传递给下一个intent。如何实现? Please ask if u need any further info. 请询问您是否需要其他信息。

TableLayout tl = (TableLayout) findViewById(R.id.tableLayout1);
        for(i=0;i<count;i++)
        {



                // Create a TableRow and give it an ID
                TableRow tr = new TableRow(this);


                // Create a TextView to house the name of the province
                TextView labelTV = new TextView(this);

                labelTV.setText(smsListDate.get(i));
                labelTV.setTextColor(Color.WHITE);


                // Create a TextView to house the value of the after-tax income
                TextView valueTV = new TextView(this);

                valueTV.setText(smsListMessage.get(i));
                tr.addView(valueTV);
                TextView valueTV2 = new TextView(this);
               valueTV.setTextColor(Color.WHITE);


               Button submit = new Button(this);
               submit.setText("respond");

               tr.addView(submit);

                // Add the TableRow to the TableLayout
                tl.addView(tr, new TableLayout.LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));

        }

Thanks 谢谢

I guess there was some gap in understanding. 我想理解上有些差距。 I do have written the submit action as 我确实将提交动作写为

submit.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                 Intent myIntent = new Intent(getApplicationContext(),
                 SmsActivity.class);

                 startActivity(myIntent);

            }});

but my intention now is to carry the values of text views in particular row to the next intent on clicking that particular button. 但是我现在的意图是将特定行中的文本视图的值传递给下一次单击该特定按钮的意图。

You will need a button listener for your submit button and in its onClick method you will need to put the value in the bundle which can get passed to the next intent. 您将需要一个按钮侦听器作为您的提交按钮,并需要在其onClick方法中将值放入可传递给下一个意图的包中。

submit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                //create an intent
                Intent intent = new Intent(ClassYouAreIn.this, 
                                           ClassForNextIntent.class);

                //put the text view value into the bundle for the intent
                //the bundle is a Map and uses key/value pairs 
                //(it is like a dictionary)
                //in this case the key is myTextViewValue 
                //and the value is valueTV.getText()
                intent.putExtra("myTextViewValue", valueTV.getText());

                //now start your new activity with the intent you just made
                startActivity(intent);

           }
});

Now in your other class (the class that the new intent will go to) you will need to access the bundle and get the value you wanted to pass to this intent. 现在在您的另一个类(新意图将进入的类)中,您将需要访问包并获取您想要传递给该意图的值。 For this example i'm going to get my bundle data in my onCreate method but you could really get the data from the bundle in any method you wanted. 对于此示例,我将在我的onCreate方法中获取捆绑包数据,但您实际上可以通过任何所需的方法从捆绑包中获取数据。

@Override
public void onCreate(Bundle bundle)
{
    super.onCreate(bundle);
    setContentView(R.layout.your_view);

    //get the extra(s) you put in the bundle
    bundle = getIntent().getExtras(); 

    String valueFromTV = bundle.getString("myTextViewValue");

    //and then do whatever you want with it
}

Hope this helps you 希望这对您有帮助

You need to create a click listener for the button and in that, create the intent and put a String extra which you get from the valueTV text. 您需要为按钮创建一个点击侦听器,然后在其中创建意图并放置一个字符串,您可以从valueTV文本中获取该字符串。

submit.setOnClickListener(new OnClickListener() {
    public void onClick(View arg0) {
        Intent i = new Intent();
    i.putExtra("value", valueTV.getText());
    startActivity(i);
}
});

Hmm... your question isn't totally clear to me, but I think what you need to do, for starters, is add an OnClickListener (I use an anonymous one) to "submit" and then create/start your intent within it's onClick(View) method. 嗯...您的问题对我来说还不是很清楚,但是我认为对于初学者来说,您需要添加一个OnClickListener(我使用匿名用户)“提交”,然后在其中创建/启动您的意图onClick(View)方法。 You can pass the value using Intent.putExtra(String, String). 您可以使用Intent.putExtra(String,String)传递值。 Something like this: 像这样:

submit.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(ThisActivity.this, NextActivity.class);
        intent.putExtra("value", valueTV.getText())
        startActivity(intent);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM