gpt4 book ai didi

java - Android Studio - 按钮小部件 OnClick。找不到函数

转载 作者:行者123 更新时间:2023-11-30 08:53:15 25 4
gpt4 key购买 nike

我希望 myButton 在单击时执行一个功能,我已经尝试过这种方法,因为它在另一个项目中对我有用,但我在这里丢失或做错了什么,因为它不起作用。

我的 XML 文件上有一个 ID 为 VazhdoButoni 的按钮,我的 java 上有一个 public gogogo (View v) {

当我转到我的 XML 文件和我的按钮属性时,在 onClick 选择框中,我没有在那里看到我的 public gogogo 函数。
该类是:

public class BikeFragment extends Fragment {

View 是:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_bike, container, false);
}

Java代码:

public void gogogo (View v) {

TextView tv = (TextView)v.findViewById(R.id.teksti2);
username = ((EditText)v.findViewById(R.id.user2)).getText().toString();
password = ((EditText)v.findViewById(R.id.pass2)).getText().toString();
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection con = DriverManager.getConnection(url, username, password);
// System.out.println("Database connection success.");

String result = "Database connection success\n";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from tblItems");
ResultSetMetaData rsmd = rs.getMetaData();

while(rs.next()) {
result += rsmd.getColumnName(1) + ": " + rs.getInt(1) + "\n";
result += rsmd.getColumnName(2) + ": " + rs.getString(2) + "\n";
result += rsmd.getColumnName(3) + ": " + rs.getString(3) + "\n";

}
tv.setText(result);
}
catch(Exception e) {
e.printStackTrace();
tv.setText(e.toString());
}
}

xml 按钮:

    <Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Vazhdo"
android:id="@+id/VazhdoButoni"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="340dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp" />

最佳答案

你错过了 onClick 标签

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Vazhdo"
android:id="@+id/VazhdoButoni"
android:onClick="gogogo"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="340dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp" />

关于java - Android Studio - 按钮小部件 OnClick。找不到函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29846447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com