猿问

具有TextView的膨胀xml Layout具有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">

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


为什么不调用该方法?我已经看到了所有其他有关使用setContent()和其他东西的线程,但是那没用,并且没有给出很好的解释。任何帮助将不胜感激。


料青山看我应如是
浏览 144回答 2
2回答

慕沐林林

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

Smart猫小萌

我想出了我的问题。或者让我的代码至少可以正常工作。一旦我对膨胀的布局有了更好的了解,我就更改了这一行代码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方法可以正常工作。
随时随地看视频慕课网APP

相关分类

Java
我要回答