复制粘贴用户输入
Copy Paste userinput
我正在做一个练习,我现在有货了。
我必须复制用户输入,然后粘贴创建的字符串。
谁能帮帮我?
这是我的代码:
public class CopyPasteActivity extends AppCompatActivity {
private Button CopyButton;
private Button PasteButton;
private EditText UserInput;
private TextView PasteText;
private final static String TAG = "CopyPasteActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_copy_paste);
CopyButton = (Button) findViewById(R.id.copy_button);
PasteButton = (Button) findViewById(R.id.paste_button);
UserInput = (EditText) findViewById(R.id.user_input);
PasteText = (TextView) findViewById(R.id.paste_text);
final String userinput = new String();
CopyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "The button Copy was pressed");
String userinput = UserInput.getText().toString();
}
});
PasteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "The button True was pressed");
PasteText.setText(userinput);
}
});
}
}
问题是您在 onCreate()
中声明了 userinput
并使其成为 final
,这会阻止您稍后对其进行编辑。同样在 CopyButton
click listener
中,您正在为 userinput
创建一个新的局部变量,其范围仅限于该侦听器。
将 String userinput = new String();
移到 onCreate()
之外,删除 final
,并将 CopyButton
侦听器中的行更改为
userinput = UserInput.getText().toString();
您不应在 CopyButton.setOnClickListener
中定义 userinput
变量。将其定义为您的 as class 或侦听器外部的实例变量,以便您的 PasteButton.setOnClickListener
应该看到它。像这样:
String userinput;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_copy_paste);
CopyButton = (Button) findViewById(R.id.copy_button);
PasteButton = (Button) findViewById(R.id.paste_button);
UserInput = (EditText) findViewById(R.id.user_input);
PasteText = (TextView) findViewById(R.id.paste_text);
CopyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "The button Copy was pressed");
userinput = UserInput.getText().toString();
}
});
PasteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "The button True was pressed");
PasteText.setText(userinput);
}
});
}
我正在做一个练习,我现在有货了。 我必须复制用户输入,然后粘贴创建的字符串。 谁能帮帮我?
这是我的代码:
public class CopyPasteActivity extends AppCompatActivity {
private Button CopyButton;
private Button PasteButton;
private EditText UserInput;
private TextView PasteText;
private final static String TAG = "CopyPasteActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_copy_paste);
CopyButton = (Button) findViewById(R.id.copy_button);
PasteButton = (Button) findViewById(R.id.paste_button);
UserInput = (EditText) findViewById(R.id.user_input);
PasteText = (TextView) findViewById(R.id.paste_text);
final String userinput = new String();
CopyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "The button Copy was pressed");
String userinput = UserInput.getText().toString();
}
});
PasteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "The button True was pressed");
PasteText.setText(userinput);
}
});
}
}
问题是您在 onCreate()
中声明了 userinput
并使其成为 final
,这会阻止您稍后对其进行编辑。同样在 CopyButton
click listener
中,您正在为 userinput
创建一个新的局部变量,其范围仅限于该侦听器。
将 String userinput = new String();
移到 onCreate()
之外,删除 final
,并将 CopyButton
侦听器中的行更改为
userinput = UserInput.getText().toString();
您不应在 CopyButton.setOnClickListener
中定义 userinput
变量。将其定义为您的 as class 或侦听器外部的实例变量,以便您的 PasteButton.setOnClickListener
应该看到它。像这样:
String userinput;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_copy_paste);
CopyButton = (Button) findViewById(R.id.copy_button);
PasteButton = (Button) findViewById(R.id.paste_button);
UserInput = (EditText) findViewById(R.id.user_input);
PasteText = (TextView) findViewById(R.id.paste_text);
CopyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "The button Copy was pressed");
userinput = UserInput.getText().toString();
}
});
PasteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "The button True was pressed");
PasteText.setText(userinput);
}
});
}