我已经制作了一个登录应用程序,它具有两个按钮:登录和注册..我必须创建另一个名为Registerclass的类并在该类中编写所有该按钮事件代码..现在我想在我的主目录中调用该按钮以获取该按钮按钮主要工作! 如何从另一个类调用按钮事件进入主事件?

这是我的主要课程代码:

    public class MainActivity extends Activity  {


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

            Registerclass rg = new Registerclass();
            View v = null; // this is not working
            rg.goButtonClicked(v); //guide me how to call a button click listener from another class i.e Registerclass
        }






        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

    }

这是我的第二个类,名为Registerclass:

    public class Registerclass extends Activity{


        @Override

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



                Button register=(Button)findViewById(R.id.bregister);
                register.setOnClickListener(new View.OnClickListener() {

                    @SuppressWarnings("deprecation")
                    @Override

                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        LinearLayout layout = new LinearLayout(Registerclass.this);
                        layout.setOrientation(LinearLayout.VERTICAL);

                        final AlertDialog alert = new AlertDialog.Builder(Registerclass.this).create();
                        final EditText login = new EditText(Registerclass.this);
                        login.setHint("Enter Login id");
                        login.setHintTextColor(Color.BLUE);
                        layout.addView(login);                          
                        final EditText pass = new EditText(Registerclass.this);
                        pass.setHint("Enter Password");
                        pass.setHintTextColor(Color.BLUE);
                        layout.addView(pass);

                        alert.setTitle("REGISTER");                         
                        alert.setView(layout);

                        //alert.setButton("Register", new DialogInterface.OnClickListener() {

                            //@Override
                            //public void onClick(DialogInterface arg0, int arg1) {
                             //TODO Auto-generated method stub
                             //String user=login.getText().toString();
                             //String pswd=pass.getText().toString();


                    //}
                //});
                        alert.show();
                    }

                });
            }


            }

这是我的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=".MainActivity"
    android:background="@drawable/background" >

    <EditText
        android:id="@+id/etlogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="186dp"
        android:background="#ffffff"
        android:ems="10"
        android:hint="Login "
        android:inputType="textEmailAddress"
        android:textSize="30dp"
        android:typeface="sans" />

    <EditText
        android:id="@+id/etpass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/etlogin"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="14dp"
        android:background="#ffffff"
        android:ems="10"
        android:hint="Password "
        android:inputType="textPassword"
        android:textSize="30dp"
        android:typeface="sans" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/blogin"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="100dp"
        android:layout_height="30dp"
        android:layout_alignLeft="@+id/etpass"
        android:layout_below="@+id/etpass"
        android:layout_marginLeft="19dp"
        android:layout_marginTop="16dp"
        android:background="#b5ee7e"
        android:text="Login" />

    <Button
        android:id="@+id/bregister"
        android:layout_width="100dp"
        android:layout_height="30dp"
        android:layout_alignBaseline="@+id/blogin"
        android:layout_alignBottom="@+id/blogin"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/blogin"
        android:background="#7EC0EE"

        android:text="Register" />

</RelativeLayout>

===============>>#1 票数:0

您可以实现的一个想法是拥有自己的点击监听器。 创建一个类,然后放置以下内容。

 Public class myCustomClickListener   implements View.onClickListener {

@Override
Public void onClick(View view) {
// put your code here 
}

然后在你的按钮上放

myButton.setOnClickListener(new MyCustomClickListener) 

===============>>#2 票数:0

为什么RegisterclassActivity 如果要在额外的类中处理onClick事件,则必须实现View.OnClickListener

主要活动:

public class MainActivity extends Activity  {

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


        Registerclass rg = new Registerclass(this);

        Button register = (Button)findViewById(R.id.bregister);
        register.setOnClickListener(rg);
    }

}

注册类别:

public class Registerclass implements View.OnClickListener{

    private final Context mContext;

    Registerclass(Context context){
        this.mContext = context;
    }

    @Override
    public void onClick(View v) {
        // Show dialog
    }
}

  ask by Mustafa Qutbuddin translate from so

未解决问题?本站智能推荐: