android - टेक्स्ट व्यू टेक्स्ट पर क्लिक या टैप कैसे करें
onclick listener (6)
मुझे पता है कि यह इतना आसान है (doh ...) लेकिन मैं एंड्रॉइड ऐप में टेक्स्ट की टेक्स्टव्यू लाइन टैप करने या क्लिक करने के तरीके को चलाने का तरीका ढूंढ रहा हूं।
मैं बटन श्रोताओं और अज्ञात विधि श्रोता कॉल के बारे में सोचता रहता हूं, लेकिन यह टेक्स्टव्यू पर लागू नहीं होता है।
क्या कोई मुझे कुछ कोड स्निपेट पर इंगित कर सकता है कि टेक्स्टव्यू में पाठ के टुकड़े पर क्लिक या टैप करने से कोई तरीका चलता है?
आप TextView के लिए TextWatcher का उपयोग कर सकते हैं, ClickLinstener से अधिक लचीला है (सबसे अच्छा या बुरा नहीं, केवल एक ही रास्ता)।
holder.bt_foo_ex.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// code during!
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// code before!
}
@Override
public void afterTextChanged(Editable s) {
// code after!
}
});
आप इन गुणों के साथ एक्सएमएल में क्लिक हैंडलर सेट कर सकते हैं:
android:onClick="onClick"
android:clickable="true"
इसके बिना क्लिक करने योग्य विशेषता को न भूलें, क्लिक हैंडलर को नहीं कहा जाता है।
main.xml
...
<TextView
android:id="@+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:textSize="55sp"
android:onClick="onClick"
android:clickable="true"/>
...
MyActivity.java
public class MyActivity extends Activity {
public void onClick(View v) {
...
}
}
टेक्स्ट के एक टुकड़े (पूरे टेक्स्ट Linkify
नहीं) पर क्लिक करने के लिए, आप Html
या Linkify
(दोनों यूआरएल खोलने वाले लिंक बना सकते हैं, हालांकि, ऐप में कॉलबैक नहीं)।
Linkify
एक स्ट्रिंग संसाधन का प्रयोग करें जैसे:
<string name="links">Here is a link: http://www..com</string>
फिर एक टेक्स्टव्यू में:
TextView textView = ...
textView.setText(R.string.links);
Linkify.addLinks(textView, Linkify.ALL);
Html
Html.fromHtml
का उपयोग करना:
<string name="html">Here you can put html <a href="http://www..com">Link!</></string>
फिर आपके टेक्स्टव्यू में:
textView.setText(Html.fromHtml(getString(R.string.html)));
टेक्स्ट व्यू में
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:onClick="onClick"
android:clickable="true"
आपको View.OnClickListener भी लागू करना होगा और ऑन क्लिक विधि में इरादा का उपयोग कर सकते हैं
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://youraddress.com"));
startActivity(intent);
मैंने परीक्षण किया कि यह समाधान ठीक काम करता है।
यह काफी नहीं हो सकता है जो आप खोज रहे हैं लेकिन यह वही है जो मैं कर रहा हूं। यह सब मेरे onCreate
बाद onCreate
:
boilingpointK = (TextView) findViewById(R.id.boilingpointK);
boilingpointK.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if ("Boiling Point K".equals(boilingpointK.getText().toString()))
boilingpointK.setText("2792");
else if ("2792".equals(boilingpointK.getText().toString()))
boilingpointK.setText("Boiling Point K");
}
});
हालांकि आप श्रोता को टेक्स्टव्यू पर सेट करके समस्या का समाधान कर सकते हैं, लेकिन इसकी अनुशंसा नहीं की जाती है। आपको फ्लैट बटन का उपयोग करना चाहिए क्योंकि यह बटन का उप-वर्ग है और यह कई विशेषताओं को प्रदान करता है जो टेक्स्टव्यू नहीं करता है।
फ्लैट बटन का उपयोग करने के लिए, style="?android:attr/borderlessButtonStyle"
बटन बटन style="?android:attr/borderlessButtonStyle"
विशेषता जोड़ें -
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DONE"
style="?android:attr/borderlessButtonStyle"/>