Is there a better way of starting a new activity on a button click than using the onClick attribute to specify a callback method as in the example below?
--- xml ---
<Button
android:id="@+id/button1"
android:onClick="someMethod"
android:text="@string/someString" />
--- java ---
public void someMethod(View view) {
startActivity(new Intent(this, SomeActivity.class));
}
I would rather like to specify the name of the activity to be started than the name of a callback method. Like this:
<Button
android:id="@+id/button1"
android:onClick="SomeActivity"
android:text="@string/someString" />
As a solution I would recommend the following. Please excuse the pseudo-like non-compiling (probably) code:
Button a = findViewById(R.id.button1);
Button b = findViewById(R.id.button2);
// Strings to be fed to the intent. You can use YourActivity.getClass() as well here
String mFirstTag = "your.first.activity.class"
String mSecondTag = "your.second.activity.class"
// This is critical as it will be used by the listener
a.setTag(mFirstTag);
b.setTag(mSecondTag);
// Self explanatory
a.setOnClickListener(mClickListener);
b.setOnClickListener(mClickListener);
// This listener will grab the "tag" object from the clicked view.
// It will be the same tag which we previously set on those buttons.
private OnClickListener mClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(v.getTag()); // Maybe you'll need toString(), or a cast here
startActivity(i);
}
}
I hope you get the general idea.
This is what I came up with thanks to Jitsu and Mike M.
The Activity to be started when a button is pressed is specified using the android:tag xml attribute and the constructor will set the onClickListeners to all buttons in the View.
This way I don't have to:
--- xml ---
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainActivity"
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="com.example.playground.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:tag="showActivity"
android:text="@string/somestring" />
</RelativeLayout>
--- java ---
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connectButtonClickListeners(findViewById(R.id.MainActivity));
}
// Walk down the View tree starting with parent and connect an onClickListener to each Button.
private void connectButtonClickListeners(View parent) {
if (parent == null)
return;
LinkedList<View> list = new LinkedList<View>();
list.add(parent);
while (list.isEmpty() == false) {
View v = list.pollFirst();
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++)
list.add(vg.getChildAt(i));
} else if (v instanceof Button) {
Button b = (Button) v;
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO: Start the new Activity.
System.out.println("Clicked!" + v.getTag());
}
});
}
}
It's simple. Subclass Button
in a separate class file, MySpecialButton.java :
public class MySpecialButton extends Button {
public MySpecialButton(Context context) {
super(context);
setupOnClickListener();
}
public MySpecialButton(Context context, AttributeSet attrs) {
super(context);
setupOnClickListener();
}
public MySpecialButton(Context context, AttributeSet attrs, int defStyle) {
super(context);
setupOnClickListener();
}
protected void setupOnClickListener() {
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(...);
}
});
}
}
I haven't compiled this code, I wrote it in a hurry, but should work.
Then in your XML, instead of Button
declare your view as com.yourpacket.yourappname.yourpackage.MySpecialButton
.
EDIT: You do need to add some things in the code, but you should manage (context reference, import statements etc.).
I like this method. No switch statement required.
Each button gets the same onClickListener
and a tag with a class name for the new activity:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startActivityForTag"
android:tag=".HelloActivity"
android:text="Say hello" >
</Button>
The click listener contructs a class from the view tag:
public void startActivityForTag(View v) {
String name = v.getTag().toString();
// if name is not fully-qualified, add the package name
if (name.startsWith(".")) {
name = getPackageName() + name;
}
try {
startActivity(new Intent(this, Class.forName(name)));
} catch (ClassNotFoundException e) { ; }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.