我的 Activity 有两个按钮。一个工作完美,一个不是。我似乎找不到问题所在。这是代码。 create_profile 按钮不起作用。谁能告诉我为什么?
private Button create_profile,select_contact;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_main);
select_contact = (Button)findViewById(R.id.add_contact);
create_profile = (Button)findViewById(R.id.create_button);
select_contact.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
final Uri uriContact = ContactsContract.Contacts.CONTENT_URI;
Intent intentPickContact = new Intent(Intent.ACTION_PICK, uriContact);
startActivityForResult(intentPickContact, PICK_CONTACT);
}
});
create_profile.setOnClickListener(new OnClickListener() {
@SuppressLint("ShowToast")
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Pressed", Toast.LENGTH_LONG);
System.out.println("PRessed");
}
});
}
这是xml代码
<Button
android:id="@+id/add_contact"
android:layout_width="85dp"
android:layout_height="wrap_content"
android:text="Add"
android:layout_marginLeft="5dp"
android:layout_marginRight="2dp"
android:textSize="19dp"/>
<Button
android:id="@+id/create_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Create Profile"
android:textSize="18sp"
android:layout_marginTop="8dp"/>
我已经检查了所有内容,但似乎找不到问题所在。
最佳答案
嗨,你为什么不显示 toast 。 toast 代码应该是这样的。
Toast.makeText(getApplicationContext(), "Pressed", Toast.LENGTH_LONG).show();
关于Android 按钮 Onclick 监听器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26825554/