0

我想使用 LayoutInflater 从第二个布局到第一个布局的任何按钮提供一个事件。

这是我的代码。

主要的.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="ScrollViewSize,TextFields,HardcodedText,UselessLeaf,UselessParent" >

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/editTextSearch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:ems="10" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp" >

            <EditText
                android:id="@+id/editTextTag"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10" />

            <Button
                android:id="@+id/buttonSave"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Save" />
        </LinearLayout>

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:text="Tagged Searches"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="match_parent"
            android:layout_height="500dp"
            android:layout_marginTop="10dp"
            android:background="#C90" >

            <TableLayout
                android:id="@+id/TableLayout1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

            </TableLayout>
        </ScrollView>

        <Button
            android:id="@+id/buttonClearTask"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Clear Tags" />

    </LinearLayout>

</TableLayout>

gonder_edit_button.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="HardcodedText,UselessParent" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center" >

    <Button
        android:id="@+id/buttonEklenecek"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="30dp"
        android:text="Add" />

    <Button
        android:id="@+id/buttonEdit"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:text="Edit" />

</LinearLayout>

还有我的java代码。

   `super.onCreate ( savedInstanceState );
    setContentView ( R.layout.main );
    buttonSave = ( Button ) findViewById ( R.id.buttonSave );
    editTextSearch = ( EditText ) findViewById ( R.id.editTextSearch );
    editTextTag = ( EditText ) findViewById ( R.id.editTextTag );
    TableLayout1 = ( TableLayout ) findViewById ( R.id.TableLayout1 );

    buttonSave.setOnClickListener ( new OnClickListener ( )
    {

        @ Override
        public void onClick ( View v )
        {

            layoutInflater = ( LayoutInflater ) getSystemService ( Context.LAYOUT_INFLATER_SERVICE );

            view = layoutInflater.inflate ( R.layout.gonder_edit_button , null );

            TableLayout1.addView ( view );

            buttonEklenen = ( Button ) view.findViewById ( R.id.buttonEklenecek );

            buttonEklenen.setText ( editTextTag.getText ( ).toString ( ) );

            buttonEklenen.setOnClickListener ( new OnClickListener ( )
            {

                @ Override
                public void onClick ( View arg0 )
                {

                    String URLstring = "http://search.twitter.com/search?p=" + buttonEklenen.getText ( ).toString ( );

                    Intent webIntent = new Intent (    Intent.ACTION_VIEW , Uri.parse ( URLstring ) );

                    startActivity ( webIntent );
                }
            } );

        }
    } );` 

例如,当我保存搜索和标记七次时,我想给一个事件第三个按钮。

4

1 回答 1

1

您应该遍历所有子视图并向它们添加侦听器:

private void logCoordinates() {
    RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout);
    for (int i = 0; i < layout.getChildCount(); i++) {
        View child = layout.getChildAt(i);
        if (child instanceof Button)
        {
           // add listeners here
        }
    }
}
于 2013-03-09T05:53:02.387 回答