除了hello world应用程序之外,这是我的第一个应用程序,所以我是一个完整的新手。首先,我的目标是从列表视图中获取预定义文本(我将在稍后阶段实现)并将其粘贴到EditText视图中(很快将在具有文本框的任何地方实现,例如SMS应用程序)。我已经掌握了listview以及如何更改和编辑文本但是我无法将所选文本呈现为EditText我已经搜索了互联网寻找答案但是已经缩短了。所以我希望stackoverflow的优秀社区可以提供帮助。下面我已经将我已经实现的所有文件都包含在这个简单应用程序的开发中。
activity_main.xml
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Paste Text"
android:id="@+id/button"
android:layout_below="@+id/textView"
android:layout_toRightOf="@+id/textView"
android:layout_toEndOf="@+id/textView"
android:onClick="PasteText"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pastedText"
android:layout_below="@+id/button"
android:layout_marginTop="62dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
SingleChoiceJava
public class SingleChoiceClass extends DialogFragment
{
final CharSequence[] items = {"Easy","Medium","Hard"};
String selection;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Choose Text To Paste...").setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
switch (arg1) {
case 0:
selection = (String) items[arg1];
break;
case 1:
selection = (String) items[arg1];
break;
case 2:
selection = (String) items[arg1];
break;
}
}
}).setPositiveButton("OK",new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(),"The Text You Pasted was:" +selection,Toast.LENGTH_LONG).show();
EditText editText = (R.id.pastedText);
editText.setText("pasted" +selection, TextView.BufferType.EDITABLE);
}
});
return builder.create();
}
}
MainActivity Class
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void PasteText(View v)
{
SingleChoiceClass my_dialog = new SingleChoiceClass();
my_dialog.show(getSupportFragmentManager(),"my_dialog");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
要将文本传入EditText,请使用editText.setText();
答案 1 :(得分:0)
你做错了。您正试图从单独的班级EditText
SingleChoiceClass
editText
将MainActivity
设为DialogFragment
的类变量,并使MainActivity
成为public class MainActivity extends FragmentActivity {
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.pastedText);
}
...
...
...
public class SingleChoiceClass extends DialogFragment {
...
...
...
//Edit your setPositiveButton() like below
}).setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(),"The Text You Pasted was:" +selection,Toast.LENGTH_LONG).show();
// Remove this line EditText editText = (R.id.pastedText);
// this is incorrect way.
// The correct way is in MainActivity onCreate()
editText.setText("pasted " +selection);
}
});
}
}
++