2015-05-16 82 views
0

我做了一個登錄應用程序,它包含兩個按鈕:登錄和註冊.. 我必須創建另一個名爲Registerclass的類並寫下該類中的所有按鈕事件代碼.. now我想在我的主窗口中調用該按鈕以獲取該按鈕在主窗口中工作!如何將另一個類的按鈕事件調用到main中?來自另一個類的調用按鈕onclick事件

這裏是我的主類代碼:

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> 
+0

您可以創建一個自定義的點擊監聽器,那麼您需要在這兩個按鈕的實現。 –

+0

請具體說明!因爲我是新的android n java都! –

回答

0

一個想法是有你自己的點擊監聽器。創建一個班級,然後放置以下內容。

Public class myCustomClickListener implements View.onClickListener { 

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

然後在你的按鈕放在

myButton.setOnClickListener(new MyCustomClickListener) 
0

爲什麼RegisterclassActivity?如果您想在額外的課程中處理onClick事件,則必須實施View.OnClickListener

MainActivity:

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); 
    } 

} 

的RegisterClass:

public class Registerclass implements View.OnClickListener{ 

    private final Context mContext; 

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

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

好的!我做了這個..但我已經通過註冊按鈕內的alertdialog中的javacode顯示一些edittext! 所以問題是...它給出的錯誤LinearLayout layout = new LinearLayout(Registerclass.this); \t \t layout.setOrientation(LinearLayout.VERTICAL); \t \t最終AlertDialog alert = new AlertDialog.Builder(Registerclass.this).create(); \t final EditText login = new EditText(Registerclass.this); –

+0

用'mContext'替換'Registerclass.this'。 – Sebastian