带有文本视图的膨胀 xml 布局具有 onClick 属性,但不会调用该属性



我有一个XML布局文件,该文件在CoordinatorLayout中具有TextView。

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".SpecificProgramSelectionActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="75dp"
android:id="@+id/saved_program"
android:text="Empty"
android:textAlignment="center"
android:gravity="fill"
android:textSize="20dp"
android:background="@drawable/program_selection_border"
android:textColor="@color/black"
android:clickable="true"
android:focusable="true"
android:onClick="addToSavedPrograms"
android:paddingTop="5dp"
android:paddingBottom="5dp"/>

此代码用于膨胀布局并将其添加到活动视图中的线性布局中。

for (PlayerWithObjectives player : players){
name = player.getName();
for (String objective : player.getObjectives()){
objectives.add(objective);
}
nameView = inflater.inflate(R.layout.inflatable_player_name_view, null);
((TextView)nameView.findViewById(R.id.saved_program)).setText(name);
((TextView)nameView.findViewById(R.id.saved_program)).setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
((TextView)nameView.findViewById(R.id.saved_program)).setTextSize(20);
linearLayout.addView(nameView);
}

(这是活动的布局 XML(

<LinearLayout 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=".SpecificProgramSelectionActivity"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/specific_program_selection_linear_layout">
</LinearLayout>
</ScrollView>

当我运行它时,应用程序中的一切看起来都很好。每个膨胀的视图都会显示,唯一的问题是我在 onClick 属性中为膨胀的 TextView 指定的方法不会被调用。为什么?这是应该调用的方法

public void addToSavedPrograms(View view){
String name = (String) (((TextView)view.findViewById(R.id.saved_program)).getText());
namesToSend.add(name);
editor.putStringSet("Tracked Players", namesToSend);
editor.commit();
Toast.makeText(getApplicationContext(),name + " was saved to preferences.", Toast.LENGTH_SHORT);
}

为什么不调用该方法?我已经看到了所有其他关于使用 setContent(( 和其他东西的线程,但这不起作用,并且在答案中没有解释那么好。任何帮助将不胜感激。

public class YourActivity extends AppCompatActivity implements OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textview=(TextView)findViewById(R.id.saved_program);
textview.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.saved_program:
//call your function here
default:
break;
}
}

你可以这样做。

更新: 首先,感谢所有评论的人,非常感谢您的帮助。您的无判断力帮助与我在这个社区中经历和看到的许多情况相比,是一个令人敬畏且受欢迎的变化。

其次,我想出了我的问题。或者至少让我的代码工作。一旦我对膨胀布局有了更好的理解,我就更改了这行代码

nameView = inflater.inflate(R.layout.inflatable_player_name_view, null);

nameView = (TextView) inflater.inflate(R.layout.inflatable_add_player_name_view, linearLayout, false);

不同之处在于,当您膨胀时,它会返回一个与指定布局文件中的顶部父级类型相同的对象(在本例中为"R.layout.inflatable_player_name_view"(。因此,我将 nameView 更改为 TextView 对象,并将返回的对象强制转换为 TextView。然后我指定了所需的父布局(以获得正确的布局参数(并输入 false,以便它不会自动将其附加到该父布局。之后,我只是对我想要的 textView 进行了更改,例如设置文本值等,然后手动将其添加到我想要的父线性布局中。完成此操作后,甚至不需要以编程方式设置onClickListener,因为android:onClick方法工作得很好。

最新更新