我做了一個登錄應用程序,它包含兩個按鈕:登錄和註冊.. 我必須創建另一個名爲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>
您可以創建一個自定義的點擊監聽器,那麼您需要在這兩個按鈕的實現。 –
請具體說明!因爲我是新的android n java都! –