[英]Inflated xml Layout with a TextView has an onClick attribute, but it does not get called.
我有一个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。 然后,我指定了所需的父布局(以获取正确的layoutParams),并设置为false,以便它不会自动将其附加到该父布局。 之后,我只需对所需的textView进行更改,例如设置文本值和诸如此类,然后将其手动添加到所需的父线性布局中。 完成此操作后,甚至不需要以编程方式设置onClickListener,因为android:onClick方法可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.