我又找了几个小时,没找到我理解/正在寻找的答案.
我有一个首选项屏幕,当用户点击菜单中的设置时会打开该屏幕.这有效.但是,当他完成设置时,我如何才能最好地使用户关闭此屏幕.
我喜欢在Chrome中完成的方式,您可以在其中返回上一个屏幕.
其他可能性也受到赞赏.
活动,属于偏好(它应该返回):
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void startGame(View view)
{
Intent intent = new Intent(this, Game.class);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.game_settings, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.action_settings:
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Run Code Online (Sandbox Code Playgroud)
喜好:
public class SettingsFragment extends PreferenceFragment
{
@Override …Run Code Online (Sandbox Code Playgroud) 我对开发应用程序很陌生。我仍然认为这是一个基本操作,所以如果已经有一个已解决的线程,我可以使用链接。但由于我为此搜索了 2 个多小时,所以我还是要问:
每次用户单击按钮时,我都想动态地将一个元素添加到我的布局中。
现在我有这个:
XML (R.layout.game.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/submit_choice"
android:onClick="submitChoice"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
爪哇
public void submitChoice(View view)
{
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText("text");
LinearLayout ll = new LinearLayout(this);
ll.addView(View.inflate(ll.getContext(), R.layout.game, null));
ll.addView(textView);
setContentView(ll);
}
Run Code Online (Sandbox Code Playgroud)
由于 XML 文件不会更改,因此它只能工作一次。
那么如何在用户第二次单击按钮时添加第二个文本(不更改 XML 文件)?示例表示赞赏。