TextView 텍스트를 클릭하거나 탭하는 방법
나는 이것이 너무 쉽다는 것을 알고 있지만 안드로이드 앱에서 TextView 텍스트 줄을 누르거나 클릭하는 방법을 찾고 있습니다.
버튼 리스너와 익명 메소드 리스너 호출에 대해 계속 생각하고 있지만 TextView에는 적용되지 않는 것 같습니다.
누군가 TextView에서 텍스트를 클릭하거나 두드리는 방법이 어떻게 실행되는지 보여주는 코드 스 니펫을 알려줄 수 있습니까?
다음 속성을 사용하여 xml에서 클릭 핸들러를 설정할 수 있습니다.
android:onClick="onClick"
android:clickable="true"
clickable 속성이 없으면 click 핸들러가 호출되지 않습니다.
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) {
...
}
}
이것은 당신이 찾고있는 것이 아닐 수도 있지만 이것이 내가하고있는 일에 효과적입니다. 이 모든 것이 내 뒤에 있습니다 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");
}
});
OK 나는 내 자신의 질문에 대답했습니다 (그러나 최선의 방법입니까?)
다음은 TextView에서 일부 텍스트를 클릭하거나 탭할 때 메소드를 실행하는 방법입니다.
package com.textviewy;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class TextyView extends Activity implements OnClickListener {
TextView t ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
t = (TextView)findViewById(R.id.TextView01);
t.setOnClickListener(this);
}
public void onClick(View arg0) {
t.setText("My text on click");
}
}
내 main.xml은 다음과 같습니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"></LinearLayout>
<ListView android:id="@+id/ListView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></ListView>
<LinearLayout android:id="@+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content"></LinearLayout>
<TextView android:text="This is my first text"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:textStyle="bold"
android:textSize="28dip"
android:editable = "true"
android:clickable="true"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
레이아웃과 텍스트 뷰를 호출하는 활동 내부 에서이 클릭 리스너는 다음과 같이 작동합니다.
setContentView(R.layout.your_layout);
TextView tvGmail = (TextView) findViewById(R.id.tvGmail);
String TAG = "yourLogCatTag";
tvGmail.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View viewIn) {
try {
Log.d(TAG,"GMAIL account selected");
} catch (Exception except) {
Log.e(TAG,"Ooops GMAIL account selection problem "+except.getMessage());
}
}
});
텍스트보기는 다음과 같이 선언됩니다 (기본 마법사).
<TextView
android:id="@+id/tvGmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/menu_id_google"
android:textSize="30sp" />
그리고 strings.xml 파일에서
<string name="menu_id_google">Google ID (Gmail)</string>
리스너를 textview로 설정하여 문제를 해결할 수 있지만 사용하지 않는 것이 좋습니다. 당신은 사용해야 평면 버튼 은 버튼의 서브 클래스이기 때문에 그것은 텍스트 뷰하지 않는 많은 특성을 제공한다.
플랫 버튼을 사용하려면 style="?android:attr/borderlessButtonStyle"
속성을 추가하십시오 -
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DONE"
style="?android:attr/borderlessButtonStyle"/>
textView에서
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:onClick="onClick"
android:clickable="true"
또한 View.OnClickListener를 구현해야하며 On Click 메서드에서 의도를 사용할 수 있습니다.
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://youraddress.com"));
startActivity(intent);
이 솔루션이 제대로 작동하는지 테스트했습니다.
To click on a piece of the text (not the whole TextView
), you can use Html
or Linkify
(both create links that open urls, though, not a callback in the app).
Linkify
Use a string resource like:
<string name="links">Here is a link: http://www.stackoverflow.com</string>
Then in a textview:
TextView textView = ...
textView.setText(R.string.links);
Linkify.addLinks(textView, Linkify.ALL);
Html
Using Html.fromHtml
:
<string name="html">Here you can put html <a href="http://www.stackoverflow.com">Link!</></string>
Then in your textview:
textView.setText(Html.fromHtml(getString(R.string.html)));
You can use TextWatcher for TextView, is more flexible than ClickLinstener (not best or worse, only more one way).
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!
}
});
참고URL : https://stackoverflow.com/questions/3328757/how-to-click-or-tap-on-a-textview-text
'development' 카테고리의 다른 글
C #을 사용하여 폴더에서 모든 파일 이름 가져 오기 (0) | 2020.05.14 |
---|---|
이 명령어 포인터에서 사용할 수 없으므로 로컬 또는 인수의 값을 얻을 수 없습니다. (0) | 2020.05.14 |
우분투에서 ANDROID_HOME 경로를 설정하는 방법은 무엇입니까? (0) | 2020.05.14 |
내 응용 프로그램 디렉토리에서 모든 .svn 디렉토리를 제거하는 방법 (0) | 2020.05.14 |
docker registry v2에서 이미지 목록을 얻는 방법 (0) | 2020.05.14 |