【发布时间】:2014-01-25 08:15:57
【问题描述】:
伙计们,我一直在挠头,试图在 TextView 上实现一个相当简单的 onClick 操作,但没有成功。这是我的代码:
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>
我的目标是能够在单击 textview 时调用 performClick() 方法。有什么建议吗?
【问题讨论】:
标签: android