ClickView TextView в Android
Я создаю приложение для Android, у которого много TextViews, которые я хочу использовать для клика.
Я попытался назначить свойства android:clickable="true"
и android:onClick="clickHandler"
единственному TextView
, и когда приложение запускает clickHandler(View v)
, я правильно получаю элемент с нажатием v.getId()
.
Мне не нравится определять для каждого TextView
свойства android:clickable
и android:onClick
... есть ли что-то, что я могу сделать с помощью кода, чтобы сказать: "все текстовые комментарии доступны для клика и щелчок обрабатывается в clickHandler
?"
Спасибо.
Ответы
Ответ 1
Вы можете сделать что-то вроде этого ниже - таким образом, у всех они есть один и тот же обработчик:
public class sticks extends Activity implements View.OnTouchListener {
private TextView tv1;
private TextView tv2;
private TextView tv3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
// bind listeners
tv1.setOnTouchListener(this);
tv2.setOnTouchListener(this);
tv3.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// check which textview it is and do what you need to do
// return true if you don't want it handled by any other touch/click events after this
return true;
}
}
Ответ 2
Я использовал это:
<TextView
android:id="@+id/txtTermsConditions"
android:layout_width="180dp"
android:layout_height="50dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:onClick="onTermConditions"
android:clickable="true"
android:focusable="true"
android:text="Terms and Conditions"
android:textColor="@color/blue" />
Ответ 3
Я использую OnClickListener, попробуйте вместо OnTouchListener. Вот описание как использовать их.
Ответ 4
Привет Вы должны использовать оба кода ниже:
<TextView
android:id="@+id/reg_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/login_btn"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:clickable="true"
android:text="Are you now need to create an account?"
android:textColor="#ff0000ff"
android:textSize="15sp"
/>
и
private TextView tv1;
tv1= (TextView)findViewById(R.id.reg_text);
tv1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
ProgressDialog progressBar = ProgressDialog.show(MainActivity.this, "Title",
"شکیبا باشید!");
progressBar.setCancelable(true);
Intent i = new Intent(getApplicationContext(), Register.class);
startActivity(i);
return false;
}
});
Ответ 5
Ниже для меня работал код:
добавить в strings.xml:
<string name="about_us"><font fgcolor="#039BE5">\"THIS IS SPARTA! more info at" <font fgcolor="#000000"><a href="#" onclick="location.href='https://google.com/'; return false;">google.com</a></font>
</string>
добавить в TextView:
android:linksClickable="true"
добавить в действие:
about_us.movementMethod = LinkMovementMethod.getInstance()