android - 在不止一个对象上添加 onclick

标签 android layout

我为我的数据库中的每个名字创建了一个按钮,然后我应该在这个按钮上添加一个 onclick 函数,但我不知道 id 使用什么。我用 ???在代码中表示位置

public class Game extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);

    ListView playersList=(ListView)findViewById(R.id.playersList);

    MyDb db=new MyDb(getApplicationContext());
    db.open();

    Cursor c=db.fetchAllUsers();
    startManagingCursor(c);

    SimpleCursorAdapter adapter=new SimpleCursorAdapter( 
            this,
            R.layout.user,
            c,
            new String[]{MyDb.UsersMetaData.USERS_NAME},
            new int[]{R.id.namePlayer}
    );

    playersList.setAdapter(adapter);
    db.close();

    Button newUser;
    newUser = (Button)findViewById(R.id.buttonNew);
    newUser.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            MyDb db=new MyDb(getApplicationContext());
            db.open();
            EditText inserted = (EditText)findViewById(R.id.editName);
            String nome = inserted.getText().toString();
            db.insertUser(nome);
            db.close();
        }
    });

    Button chosen;
    chosen = (Button)findViewById(R.id.???);
    chosen.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent = new Intent(view.getContext(), Game2.class);
            startActivityForResult(myIntent, 0);
            finish();
        }
    });
    }

}

用户界面

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/sfondo"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:background="@drawable/sfondo">

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

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"> 

            <EditText
                android:id="@+id/editName"
                android:text="@string/editName"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:lines="1"
                android:width="180px" />
        <Button
                android:text="@string/newPlayer"
                android:id="@+id/buttonNew"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:minWidth="70px" />
        </LinearLayout>
        <ListView
            android:id="@+id/playersList"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>
</FrameLayout>

db 元素的 xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:layout_gravity="center_vertical"
    >

    <Button
        android:id="@+id/namePlayer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:minWidth="250px" />
</LinearLayout>

谢谢

最佳答案

所以按钮在 R.layout.user 中?如果是这样,我认为您可能需要扩展 SimpleCursorAdapter 并且在 getView() 方法中您可以找到 View 并添加监听器。像这样:

public class MyCursorAdapter extends SimpleCursorAdapter {
    public MyCursorAdapter(...) {
        super(...);
    }

    public void getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        Button button = (Button) view.findViewById(R.id.???);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                ...
            }
        });
    }
}

这样,您的“查找”范围就限定在列表中的一行。您不能在当前拥有代码的位置添加监听器,因为会有多个具有相同 ID 的 View 。

关于android - 在不止一个对象上添加 onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6181180/

相关文章:

java - Android Afreechart - 更改点形状、线条粗细和颜色

android - 如何在 Android 中将 PDF 转换为 HTML 页面?

java - 如果 Android Activity 位于前台,则向其传递通知 Intent 并启动它的适当方法是什么?

java - 如何设置gridlayout jpanel的大小

css - 布局 flexbox : how to get the leaf nodes centered vertically and horizontally within the half-containers?

android - TextView 中的多个彩色 <a> 标签

安卓游戏开发 : Collision Detection Failing

jquery - 应用所有 CSS 规则后执行 jQuery 代码

c++ - Qt 防止控件在调整窗口大小时移动

android - Android 键盘上方的工具栏