Fellas,我一直在撓頭,試圖在TextView上實現一個相當簡單的onClick動作,但沒有成功。這裏是我的代碼:Android onClick Action
public class AccountsActivity extends Activity {
final Context context = this;
private TextView tvNextOkin;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_account);
tvNextOkin = (TextView) findViewById(R.id.tv_acc_next_of_kin);
tvNextOkin.setText("Not set. Tap here to add");
tvNextOkin.setTextColor(Color.RED);
}
public void performClick(View view){
Log.i("Action::", "clicked!!");
// add listener
tvNextOkin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("This is a custom dialog");
dialog.show();
}
});
}
}
這裏是XML佈局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="2dp" >
<TextView
android:id="@+id/tv_acc_next_of_kin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="tap here to add"
android:textColor="#000"
android:textSize="14dp"
android:onClick="performClick"
android:typeface="sans" />
</LinearLayout>
</ScrollView>
我的目標是能夠調用performClick()方法被點擊TextView的時候。有什麼建議麼?
謝謝droidx。我很欣賞 – user2251344
你歡迎@ user2251344。如果您發現ans有用,請投票:-)哦,謝謝您已經這麼做了 – droidx