我正在尝试编写一个简单的待办事项列表,包括一个标题屏幕(这是一个主要 Activity )和一个切换到实际列表(这是另一个 Activity )的按钮。但是,在另一个 Activity 上单击“添加”按钮后,什么也没有发生——甚至没有调用 onClick 函数。我该如何完成这项工作?
我已尝试根据此解决方案重写代码,但没有任何改变:Android Studio onClick and OnclickListener not working once changed to another Activity 此外,我尝试使用此处提到的第一种方法来实现代码,但无济于事: https://medium.com/@CodyEngel/4-ways-to-implement-onclicklistener-on-android-9b956cbd2928
这是 MainActivity 类:
package com.example.kandydatpl;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void openChecklist(View view) {
Intent intent = new Intent(this, TaskListActivity.class);
startActivity(intent);
}
}
主 Activity .xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="openChecklist"
android:text="Open Checklist"
tools:layout_editor_absoluteX="136dp"
tools:layout_editor_absoluteY="407dp" />
</android.support.constraint.ConstraintLayout>
以及 TaskListActivity 类:
package com.example.kandydatpl;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
public class TaskListActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
private EditText itemET;
private Button btn;
private ListView itemsList;
private ArrayList<String> items;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_task_list);
itemET = findViewById(R.id.item_edit_text);
btn = findViewById(R.id.add_btn);
itemsList = findViewById(R.id.items_list);
items = FileHelper.readData(this);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
itemsList.setAdapter(adapter);
btn.setOnClickListener(this);
itemsList.setOnItemClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.add_btn:
String itemEntered = itemET.getText().toString();
adapter.add(itemEntered);
itemET.setText("");
FileHelper.writeData(items, this);
Toast.makeText(this, "Item Added!", Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
items.remove(position);
adapter.notifyDataSetChanged();
FileHelper.writeData(items, this);
Toast.makeText(this, "Delete", Toast.LENGTH_SHORT).show();
}
}
和相应的 XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".TaskListActivity">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/item_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:hint="Enter Item" />
<Button
android:id="@+id/add_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClickAdd"
android:text="Add" />
</LinearLayout>
<ListView
android:id="@+id/items_list"
android:layout_width="395dp"
android:layout_height="667dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout"
tools:layout_editor_absoluteX="8dp" />
</android.support.constraint.ConstraintLayout>
最佳答案
由于您在 xml 文件中使用了 android:onClick="onClickAdd
,因此您需要将 onClickAdd
方法添加到您的 TaskListActivity
中,如下所示如下:
public void onClickAdd(View view)
{
switch (view.getId())
{
case R.id.add_btn:
String itemEntered = itemET.getText().toString();
adapter.add(itemEntered);
itemET.setText("");
FileHelper.writeData(items, this);
Toast.makeText(this, "Item Added!", Toast.LENGTH_SHORT).show();
break;
}
}
当您在 xml 文件中使用 onClick
属性时,您的 TaskListActivity
不需要实现 View.OnClickListener
接口(interface),并且因此您不需要将 onClickListener
添加到您的 Button
。
关于java - 更改 Activity 时按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58421687/