如何从Fragment中找到id按钮

时间:2017-01-26 01:22:12

标签: java android android-studio

我正在制作一个Android应用程序,我有一个按钮,当我按下它时不会做出反应。该按钮应该会显示一个警报对话框。我非常困惑,因为我有一个OnClickListener并且实例化了对话框。

以下是Java文件中的代码:

public class tab1Expenses extends Fragment {

    Button btnEx;

    public class button extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.tab1expense);
            btnEx = (Button)findViewById(R.id.btnEx);

            btnEx.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    View view = LayoutInflater.from(button.this).inflate(R.layout.add_ex, null);

                    AlertDialog.Builder add = new AlertDialog.Builder(button.this);
                    add.setView(view);

                    add.setCancelable(true)
                        .setTitle("Enter Expense:")
                        .setMessage("Expense Name:")
                        .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });

                    Dialog dialog = add.create();
                    dialog.show();
                }
            });
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.tab1expense, container, false);
        return rootView;
    }

}

这是我的XML文件与java文件相关:

<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.ojemz.expensetracker.tab1Expenses$button"
android:background="@android:color/darker_gray">

<Button
    android:text="Add Expense"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btnEx"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:width="800dp"
    android:textAppearance="@style/TextAppearance.AppCompat.Widget.Switch"
    android:background="@android:color/black"
    android:textColor="@android:color/holo_green_light" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="17dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <!-- TextView and other stuff -->

    </LinearLayout>
</ScrollView>

1 个答案:

答案 0 :(得分:1)

你做错了!

扩展button的{​​{1}}类没有理由在这里存在。

如果您只想获得一些Activity回调,请执行以下操作:

onClick

别忘了删除你的&#34;按钮&#34;内心阶层。

旁注

您应该将@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.tab1expense, container, false); btnEx = (Button)rootView.findViewById(R.id.btnEx); btnEx.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { View view = LayoutInflater.from(button.this).inflate(R.layout.add_ex, null); AlertDialog.Builder add = new AlertDialog.Builder(tab1Expenses.this.getActivity()); add.setView(view); add.setCancelable(true) .setTitle("Enter Expense:") .setMessage("Expense Name:") .setPositiveButton("Add", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); Dialog dialog = add.create(); dialog.show(); } }); return rootView; } tab1Expenses类重命名为Fragment,以便区分实例和类。

请参阅Java naming conversions