复制到剪贴板 - Android studio (getString - settextView)
Copy to clipboard - Android studio (getString - settextView)
我正在尝试向 settextview 添加“复制”按钮。
这是我的完整代码:
public class activity_tasks extends AppCompatActivity {
private Button button_refresh;
TextView textView;
TextView textView4;
private AdView mAdView;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tasks);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
button_refresh = findViewById(R.id.button_refresh);
button_refresh.setOnClickListener(v -> {
finish();
startActivity(getIntent());
overridePendingTransition(0, 0);
});
Button button = findViewById(R.id.button1);
Button button_copy = findViewById(R.id.button_copy);
button.setOnClickListener(v -> showInfo());
textView = findViewById(R.id.textView4);
downloadJSON();
}
private void downloadJSON() {
@SuppressLint("StaticFieldLeak")
class DownloadJSON extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
try {
settextView(s);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(Void... voids) {
try {
URL url = new URL("https://www.URL.app/api/tasks.php");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json).append("\n");
}
return sb.toString().trim();
} catch (Exception e) {
return null;
}
}
}
DownloadJSON getJSON = new DownloadJSON();
getJSON.execute();
}
private void settextView(String json) throws JSONException {
JSONArray jsonArray = new JSONArray(json);
String[] tasks = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
tasks[i] = obj.getString("Task_title") + ".\n\n" + obj.getString("task_description");
textView.setText(tasks[0]);
}
我的问题是我不确定将以下代码放在哪里:
button_copy.setOnClickListener(v -> {
ClipboardManager clipboard = (ClipboardManager)getApplicationContext().getSystemService(getApplicationContext().CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("", obj.getString("Task_title") + ".\n\n" + obj.getString("task_description"));
clipboard.setPrimaryClip(clip);
Toast.makeText(getApplicationContext(), "Copied to clipboard!", Toast.LENGTH_LONG).show();
});
我试过将它放在 settextView 中,但是当我登陆页面并生成文本时,它会自动复制 - 但我希望它归因于一个按钮,而不是自动复制。
作为参考,我的 activity_list.xml 中的按钮是 button_copy.
为了更方便,将按钮放在 settextview
内。当进程还在进行时,我们需要用按钮hide/do一些东西。您可以在为按钮创建 .setVisibility
时隐藏它。之后,您可以显示按钮。
private void settextView(String json) throws JSONException {
JSONArray jsonArray = new JSONArray(json);
String[] tasks = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
tasks[i] = obj.getString("Task_title") + ".\n\n" + obj.getString("task_description");
textView.setText(tasks[0]);
button_copy.setVisibility(View.VISIBLE); //Visible it
setBtnCopyOnClick(tasks[0]); //Here
}
然后为btn创建一个方法
private void setBtnCopyOnClick(String text){
button_copy.setOnClickListener(v -> {
ClipboardManager clipboard = (ClipboardManager)getApplicationContext().getSystemService(getApplicationContext().CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("", text);
clipboard.setPrimaryClip(clip);
Toast.makeText(getApplicationContext(), "Copied to clipboard!", Toast.LENGTH_LONG).show();
});
}
感谢 Ticherhaz 让我到达那里!
按照他的指示,问题是字符串的属性不正确,所以新代码是:
private void setBtnCopyOnClick(String task) {
Button button_copy = findViewById(R.id.button_copy);
button_copy.setOnClickListener(v -> {
ClipboardManager clipboard = (ClipboardManager)getApplicationContext().getSystemService(getApplicationContext().CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("", task);
clipboard.setPrimaryClip(clip);
Toast.makeText(getApplicationContext(), "Copied to clipboard!", Toast.LENGTH_LONG).show();
});
我正在尝试向 settextview 添加“复制”按钮。
这是我的完整代码:
public class activity_tasks extends AppCompatActivity {
private Button button_refresh;
TextView textView;
TextView textView4;
private AdView mAdView;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tasks);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
button_refresh = findViewById(R.id.button_refresh);
button_refresh.setOnClickListener(v -> {
finish();
startActivity(getIntent());
overridePendingTransition(0, 0);
});
Button button = findViewById(R.id.button1);
Button button_copy = findViewById(R.id.button_copy);
button.setOnClickListener(v -> showInfo());
textView = findViewById(R.id.textView4);
downloadJSON();
}
private void downloadJSON() {
@SuppressLint("StaticFieldLeak")
class DownloadJSON extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
try {
settextView(s);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(Void... voids) {
try {
URL url = new URL("https://www.URL.app/api/tasks.php");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json).append("\n");
}
return sb.toString().trim();
} catch (Exception e) {
return null;
}
}
}
DownloadJSON getJSON = new DownloadJSON();
getJSON.execute();
}
private void settextView(String json) throws JSONException {
JSONArray jsonArray = new JSONArray(json);
String[] tasks = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
tasks[i] = obj.getString("Task_title") + ".\n\n" + obj.getString("task_description");
textView.setText(tasks[0]);
}
我的问题是我不确定将以下代码放在哪里:
button_copy.setOnClickListener(v -> {
ClipboardManager clipboard = (ClipboardManager)getApplicationContext().getSystemService(getApplicationContext().CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("", obj.getString("Task_title") + ".\n\n" + obj.getString("task_description"));
clipboard.setPrimaryClip(clip);
Toast.makeText(getApplicationContext(), "Copied to clipboard!", Toast.LENGTH_LONG).show();
});
我试过将它放在 settextView 中,但是当我登陆页面并生成文本时,它会自动复制 - 但我希望它归因于一个按钮,而不是自动复制。 作为参考,我的 activity_list.xml 中的按钮是 button_copy.
为了更方便,将按钮放在 settextview
内。当进程还在进行时,我们需要用按钮hide/do一些东西。您可以在为按钮创建 .setVisibility
时隐藏它。之后,您可以显示按钮。
private void settextView(String json) throws JSONException {
JSONArray jsonArray = new JSONArray(json);
String[] tasks = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
tasks[i] = obj.getString("Task_title") + ".\n\n" + obj.getString("task_description");
textView.setText(tasks[0]);
button_copy.setVisibility(View.VISIBLE); //Visible it
setBtnCopyOnClick(tasks[0]); //Here
}
然后为btn创建一个方法
private void setBtnCopyOnClick(String text){
button_copy.setOnClickListener(v -> {
ClipboardManager clipboard = (ClipboardManager)getApplicationContext().getSystemService(getApplicationContext().CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("", text);
clipboard.setPrimaryClip(clip);
Toast.makeText(getApplicationContext(), "Copied to clipboard!", Toast.LENGTH_LONG).show();
});
}
感谢 Ticherhaz 让我到达那里!
按照他的指示,问题是字符串的属性不正确,所以新代码是:
private void setBtnCopyOnClick(String task) {
Button button_copy = findViewById(R.id.button_copy);
button_copy.setOnClickListener(v -> {
ClipboardManager clipboard = (ClipboardManager)getApplicationContext().getSystemService(getApplicationContext().CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("", task);
clipboard.setPrimaryClip(clip);
Toast.makeText(getApplicationContext(), "Copied to clipboard!", Toast.LENGTH_LONG).show();
});