我已经创建了一个登录应用程序,它有两个按钮:登录和注册.. 我必须创建另一个名为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>
答案 0 :(得分:0)
您可以实现的一个想法就是拥有自己的点击监听器。创建一个类,然后放入以下内容。
Public class myCustomClickListener implements View.onClickListener {
@Override
Public void onClick(View view) {
// put your code here
}
然后按下按钮
myButton.setOnClickListener(new MyCustomClickListener)
答案 1 :(得分:0)
为什么Registerclass
为Activity
?如果您想在额外课程中处理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
}
}