[英]Is there a better way of starting a new activity on button click that calling an onClickListener?
是否有比使用onClick屬性指定回調方法更好的方法來啟動按鈕單擊上的新活動,如下面的示例所示?
-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));
}
我想指定要啟動的活動的名稱,而不是回調方法的名稱。 像這樣:
<Button
android:id="@+id/button1"
android:onClick="SomeActivity"
android:text="@string/someString" />
作為解決方案,我建議以下內容。 請原諒偽類非編譯(可能)代碼:
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);
}
}
希望您能大致了解。
這就是我對Jitsu和Mike M的感謝。
使用android:tag xml屬性指定按下按鈕時要啟動的Activity,構造函數會將onClickListeners設置為View中的所有按鈕。
這樣,我不必:
-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());
}
});
}
}
這很簡單。 一個單獨的類文件MySpecialButton.java中的Subclass Button
:
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(...);
}
});
}
}
我尚未編譯此代碼,我急忙編寫了它,但應該可以工作。
然后在您的XML中,而不是Button
將您的視圖聲明為com.yourpacket.yourappname.yourpackage.MySpecialButton
。
編輯:您確實需要在代碼中添加一些內容,但是您應該進行管理(上下文引用,導入語句等)。
我喜歡這種方法。 無需switch語句。
每個按鈕都具有相同的onClickListener
和帶有新活動的類名稱的標簽:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startActivityForTag"
android:tag=".HelloActivity"
android:text="Say hello" >
</Button>
點擊偵聽器通過view標記構造一個類:
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) { ; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.