单击textview时显示Toast

时间:2014-08-05 11:49:22

标签: android textview toast

在此示例中,我尝试点击两个文本视图(mLayout1 and mLayout2)中的每一个, 并祝酒会出现。代码编译时没有错误,但在运行时没有显示吐司。

这是我的代码:

public class MainActivity extends ActionBarActivity {

private TextView mLayout1, mLayout2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        View view = inflater.inflate(R.layout.activity_main,

        mLayout1 = (TextView) view.findViewById(R.id.item_1a);
        mLayout1.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(),"mLayout1 clicked",Toast.LENGTH_SHORT).show();
            }

        });

        mLayout2 = (TextView) view.findViewById(R.id.item_1b);
        mLayout2.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(),"mLayout2 clicked",Toast.LENGTH_SHORT).show();
            }

        });

        return view;
    }
}

这是我的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.transitionexample.MainActivity" >


        <TextView
            android:id="@+id/item_1a"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Item 1a"
            android:background="@color/green"
            android:gravity="center"/>

        <TextView
            android:id="@+id/item_1b"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="74dp"
            android:gravity="center"
            android:background="@color/opaque_red"
            android:text="Item 1b" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

试试这个:

android:clickable="true"

<TextView
    android:clickable="true"
    android:id="@+id/item_1a"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Item 1a"
    android:background="@color/green"
    android:gravity="center"/>

答案 1 :(得分:0)

你的onCreateView永远不会被调用 - 这不是片段 - 添加@Override注释,你会看到......