片段中的setOnClickListener [英] setOnClickListener in fragment

查看:44
本文介绍了片段中的setOnClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的应用程序,它有1个Edittext和1个Button.我的应用程序获取前两个EditText中的数字,当按下按钮时,最后一个EditText显示前两个EditText的总和.所有这些都与Activity有关,但我想对fragment.I具有

I have simple application which has 1 Edittext and 1 Button.My application get the numbers in the first two Edit text and when the button is pressed the last EditText show the sum of the first Two EditText.Until now, I have done all this with Activity, but I want to do this with fragment.I have this source code, that have navigation Drawer, andwhen try to do the same in this code I have problems with setOnClicklistener

这是我的代码:

package info.androidhive.slidingmenu;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

public class FindPeopleFragment extends Fragment implements OnClickListener {
    EditText Num1, Num2, Eq;
    Button Sum;
    int S;

    public FindPeopleFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    { 
        View InputFragmentView = inflater.inflate(R.layout.fragment_find_people, container, false);
        Num1 = (EditText) InputFragmentView.findViewById(R.id.Num1);
        Num2 = (EditText) InputFragmentView.findViewById(R.id.Num2);
        Eq = (EditText) InputFragmentView.findViewById(R.id.Eq);
        Sum = (Button) InputFragmentView.findViewById(R.id.Sum);

        Sum.setOnClickListener(new OnClickListener(){
            public void onClick(View view)
            {
                String N1 = Num1.getText().toString();
                int N11 = Integer.parseInt(N1);

                String N2 = Num2.getText().toString();
                int N22 = Integer.parseInt(N2);

                S = N11 + N22;

                Eq.setText("" + S);

            }
        });

        return InputFragmentView;
    }
}

推荐答案

package info.androidhive.slidingmenu;

import android.app.Fragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

 public class FindPeopleFragment extends Fragment {
EditText Num1, Num2, Eq;
Button Sum;
int S;

public FindPeopleFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    super.onCreateView(inflater, container, savedInstanceState);
    View InputFragmentView = inflater.inflate(
            R.layout.fragment_find_people, container, false);
    Num1 = (EditText) InputFragmentView.findViewById(R.id.Num1);
    Num2 = (EditText) InputFragmentView.findViewById(R.id.Num2);
    Eq = (EditText) InputFragmentView.findViewById(R.id.Eq);
    Sum = (Button) InputFragmentView.findViewById(R.id.Sum);
    ;

    Sum.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String N1 = Num1.getText().toString();
            int N11 = Integer.parseInt(N1);

            String N2 = Num2.getText().toString();
            int N22 = Integer.parseInt(N2);

            S = N11 + N22;

            Eq.setText("" + S);
            Toast.makeText(getActivity(), "your Sum is : " + S,
                    Toast.LENGTH_LONG).show();
        }
    });
    return InputFragmentView;
}
}

这篇关于片段中的setOnClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆