Как дать id и метод onclick динамически создаваемой кнопке
package in.confio.www.dynamictext; import android.annotation.SuppressLint; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.view.menu.ListMenuPresenter; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private LinearLayout mLayout; private EditText mEditText; private Button mButton; Integer No_Buttons; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mLayout = (LinearLayout) findViewById(R.id.liner); mEditText = (EditText) findViewById(R.id.editText); mButton = (Button) findViewById(R.id.button); mButton.setOnClickListener(onClick()); } private View.OnClickListener onClick() { return new View.OnClickListener() { @SuppressLint("ResourceType") @Override public void onClick(View v) { mLayout.addView(createNewButton(mEditText.getText().toString())); mEditText.setId(); mEditText.setOnClickListener(); } }; } private Button createNewButton(String text) { final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); final Button button = new Button(this); button.setLayoutParams(lparams); button.setText("New Room: " + text); return button; } }
Что я уже пробовал:
<?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:orientation="vertical" android:id="@+id/liner" android:layout_width="fill_parent" android:layout_height="fill_parent" tools:context="in.confio.www.dynamictext.MainActivity"> <EditText android:id="@+id/editText" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add+" /> </LinearLayout>
David Crow
Что не работает? Вы получаете какую-то ошибку?