我的按钮在 android 工作室中没有响应
My Button is not responding in android studio
当我 运行 应用程序时。按钮拒绝响应。我设置了 Toast 和 logcat 来检查响应。但非。请帮忙解决
这是我的源代码
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:id="@+id/button"
tools:onClick="topClick" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:id="@+id/button2"
tools:onClick="bottomClick" />
以及我的 java 方法;
public class MyActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
//Testing the Toast and Log in action
Toast.makeText(this, "Can you see me?", Toast.LENGTH_SHORT).show();
Log.i("info", "Done creating the app");
}
//creating method topclick
public void topClick(View v) {
Toast.makeText(this, "Top button clicked", Toast.LENGTH_SHORT).show();
Log.i("info", "The user clicked the top button");
}
//creating method bottomclick
public void bottomClick(View v) {
Toast.makeText(this, "Bottom button clicked", Toast.LENGTH_SHORT).show();
Log.i("info", "the user clicked the bottom button");
}
}
替换
tools:onClick
和
android:onClick
两个按钮。
有关有助于您理解为什么需要进行此更改的更多上下文,请阅读 tools
namespace:
Android Studio supports a variety of XML attributes in the tools namespace that enable design-time features (such as which layout to show in a fragment) or compile-time behaviors (such as which shrinking mode to apply to your XML resources). When you build your app, the build tools remove these attributes so there is no effect on your APK size or runtime behavior.
我强烈不建议使用您的那个实现,因为您几乎不知道哪个按钮对应哪个方法。试试这个。
XML
<Button
android:text="Button"
android:id="@+id/button1" />
<Button
android:text="Button"
android:id="@+id/button2" />
ACTIVITY.JAVA
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
Button button01 = (Button)findViewById(R.id.button01);
Button button02 = (Button)findViewById(R.id.button02);
button01 .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MyActivity.this, "This is button01", Toast.LENGTH_SHORT).show();
}
});
button02 .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MyActivity.this, "This is button02", Toast.LENGTH_SHORT).show();
}
});
}
当我 运行 应用程序时。按钮拒绝响应。我设置了 Toast 和 logcat 来检查响应。但非。请帮忙解决
这是我的源代码
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:id="@+id/button"
tools:onClick="topClick" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:id="@+id/button2"
tools:onClick="bottomClick" />
以及我的 java 方法;
public class MyActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
//Testing the Toast and Log in action
Toast.makeText(this, "Can you see me?", Toast.LENGTH_SHORT).show();
Log.i("info", "Done creating the app");
}
//creating method topclick
public void topClick(View v) {
Toast.makeText(this, "Top button clicked", Toast.LENGTH_SHORT).show();
Log.i("info", "The user clicked the top button");
}
//creating method bottomclick
public void bottomClick(View v) {
Toast.makeText(this, "Bottom button clicked", Toast.LENGTH_SHORT).show();
Log.i("info", "the user clicked the bottom button");
}
}
替换
tools:onClick
和
android:onClick
两个按钮。
有关有助于您理解为什么需要进行此更改的更多上下文,请阅读 tools
namespace:
Android Studio supports a variety of XML attributes in the tools namespace that enable design-time features (such as which layout to show in a fragment) or compile-time behaviors (such as which shrinking mode to apply to your XML resources). When you build your app, the build tools remove these attributes so there is no effect on your APK size or runtime behavior.
我强烈不建议使用您的那个实现,因为您几乎不知道哪个按钮对应哪个方法。试试这个。
XML
<Button
android:text="Button"
android:id="@+id/button1" />
<Button
android:text="Button"
android:id="@+id/button2" />
ACTIVITY.JAVA
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
Button button01 = (Button)findViewById(R.id.button01);
Button button02 = (Button)findViewById(R.id.button02);
button01 .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MyActivity.this, "This is button01", Toast.LENGTH_SHORT).show();
}
});
button02 .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MyActivity.this, "This is button02", Toast.LENGTH_SHORT).show();
}
});
}