【问题标题】:pass data using putExtra and use it in onClick使用 putExtra 传递数据并在 onClick 中使用
【发布时间】:2020-12-06 21:56:18
【问题描述】:

我写了以下代码:

public class HoursActivity extends AppCompatActivity{

    public String name;
    public String number;
    public String date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hours);
        Bundle extras = getIntent().getExtras();

        //if (extras != null) {
            name = extras.getString("name");
            number = extras.getString("number");
            date = extras.getString("date");
        //}
        final int hour = 9;
        //final int minute = 0;
        for (int i = 1; i <= 10; i++) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            Button btn = new Button(this);
            btn.setId(hour);
            final int id_ = btn.getId();
            btn.setText(hour + i +":00");
            LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
            linearLayout.addView(btn, params);
            final Button btn1 = ((Button) findViewById(id_));
            btn1.setOnClickListener(new View.OnClickListener() {
                @RequiresApi(api = Build.VERSION_CODES.O)
                public void onClick(View view) {
                    Toast.makeText(view.getContext(),
                            date + name + number, Toast.LENGTH_SHORT)
                            .show();
                }
            });
        }
    }

这段代码创建了 10 个按钮,当我单击一个按钮时,我想打印从上一个活动中获得的数据。于是,我写了如下代码:

            name = extras.getString("name");
            number = extras.getString("number");
            date = extras.getString("date");

我为每个按钮编写了这段代码:

public void onClick(View view) {
                    Toast.makeText(view.getContext(),
                            date + name + number, Toast.LENGTH_SHORT)
                            .show();
                }

问题是字符串 date + name + number 只为第一个按钮打印。 我哪里做错了?

【问题讨论】:

  • 您应该在btn 上设置点击监听器。取消btn1
  • For each button I wrote this code: 您不必重复。我们已经看到了。
  • 谢谢你,成功了:)

标签: android android-studio button android-intent android-activity


【解决方案1】:

创建意图:

Intent i = new Intent(MainActivity.this, SecondActivity.class);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
String getText=textView.getText().toString();

//Create the bundle
Bundle bundle = new Bundle();

//Add your data to bundle
bundle.putString(“getText”, getText);

//Add the bundle to the intent
i.putExtras(bundle);

//Fire that second activity
startActivity(i);

然后转到您的第二个活动,从包中检索您的数据:

 //Get the bundle
    Bundle bundle = getIntent().getExtras();
    
    //Extract the data…
    String stuff = bundle.getString(“stuff”); 

【讨论】:

    猜你喜欢
    • 2018-01-15
    • 2018-08-27
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 2021-09-24
    • 2023-03-26
    • 2020-04-25
    • 2020-10-13
    相关资源
    最近更新 更多