我有一个带有一些 fragment 等的小应用程序。现在每次我改变屏幕的方向时, Action 都会重新开始,这意味着如果你转动你的设备,输入就会丢失。我怎样才能防止这种情况发生?此外,我想保留更改方向的可能性,应用程序也会随之更改,我只想保留内容。
魔术发生在 MainActivity.java 中,显示内容在 SearchFragment.xml 中
主要 Activity .java:
package mypackage;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "mypackage.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//addListenerOnSpinnerItemSelection();
// lädt die ActionBar
ActionBar actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.setDisplayShowHomeEnabled(true);
actionbar.setDisplayShowTitleEnabled(true);
actionbar.setDisplayShowCustomEnabled(false);
// lädt die Tabs in die Actionbar
String search = getString(R.string.search_fragment);
String user = getString(R.string.user_fragment);
String recent = getString(R.string.recent_fragment);
ActionBar.Tab TabA = actionbar.newTab().setText(search);
ActionBar.Tab TabB = actionbar.newTab().setText(user);
ActionBar.Tab TabC = actionbar.newTab().setText(recent);
// erstellt neue Fragmente
Fragment fragmentA = new SearchFragment();
Fragment fragmentB = new UserFragment();
Fragment fragmentC = new UpdateFragment();
// Listener werden angelegt
TabA.setTabListener(new MyTabsListener(fragmentA));
TabB.setTabListener(new MyTabsListener(fragmentB));
TabC.setTabListener(new MyTabsListener(fragmentC));
// Tabs werden in die actionbar geladen
actionbar.addTab(TabA);
actionbar.addTab(TabB);
actionbar.addTab(TabC);
}
public void sendMessage(View view)
{
Spinner mySpinner = (Spinner) findViewById(R.id.criterion);
String text = mySpinner.getSelectedItem().toString();
//manual initialation of array - later to be created by XML-Parser
String[][] array = {{"Opel", "Astra", "2010", "120 PS", "12 l/100km", "Blau, Rot, Grün, Braun", "File 1", "File 2"},
{"Opel", "Corsa", "2012", "80 PS", "8 l/100km", "Grün, Rot, Blau, Gelb, Magenta", "File 1", "File 2", "File 3"},
{"Opel","Vectra", "1980", "200 PS" ,"20 l/100km", "Braun", "file 1"},
{"Opel", "Astra", "2010", "120 PS", "12 l/100km", "Blau, Rot, Grün, Braun", "File 1", "File 2"},
{"Opel", "Corsa", "2012", "80 PS", "8 l/100km", "Grün, Rot, Blau, Gelb, Magenta", "File 1", "File 2", "File 3"},
{"Opel","Vectra", "1980", "200 PS" ,"20 l/100km", "Braun", "file 1"} };
// Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String output = "";
//add Elements of Array on output string
for(int i = 0; i <array.length; i++)
{
int count = 0;
//Standard values
for(int j = 0; j<6; j++)
{
output = output + array[i][j];
if(count<5)
{
output = output + " - ";
}
count++;
}
output = output + "\n";
int count2 = 6;
//User generated values with diferent length
for(int k = 6; k <array[i].length; k++)
{
output = output + array[i][k];
if(count2<array[i].length-1)
{
output = output + " - ";
}
count2++;
}
output = output + "\n\n";
}
String message = getText(R.string.search_text)+" "+text+": "+ editText.getText().toString() + "\n\n"+output;
//display results
TextView viewText1 = (TextView) findViewById(R.id.showResults);
viewText1.setText(message);
//Clear EditText field
editText.setText("");
//Hides Softkeyboard on "Send" Button
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
// intent.putExtra(EXTRA_MESSAGE, message);
// startActivity(intent);
}
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// wenn das Tab erneut gewŠhlt wird.
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
}
@Override //creates the action bar menu using the text information from strings.xml and the method menu() from R.java
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
return true; //number of points in overflow menu or sth like this
}
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_settings:
{
Intent intent = new Intent(this, Settings.class);
startActivity(intent);
break;
}
case R.id.about_section:
{
Intent intent = new Intent(this, About.class);
startActivity(intent);
break;
}
case R.id.faq_section:
{
Intent intent = new Intent(this, FAQ.class);
startActivity(intent);
break;
}
case R.id.search_section:
{
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
break;
}
}
return true;
}
}
搜索 fragment .xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/welcome" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal" >
<EditText
android:id="@+id/edit_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="@string/edit_message" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp" >
<Spinner
android:id="@+id/criterion"
android:layout_width="117dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:entries="@array/array"
android:prompt="@string/prompt" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="136dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:layout_height="match_parent"
android:layout_gravity="clip_vertical"
android:onClick="sendMessage"
android:text="@string/button_send"
android:background="@drawable/android" />
</LinearLayout>
<TextView
android:id="@+id/showResults"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
最佳答案
据我所知,有一个用于此方向更改的监听器,因此您可以在方向更改之前保存一些数据,因为当方向更改 Activity 在内部重新启动时。
查看 http://developer.android.com/reference/android/view/OrientationListener.html
那是类(class)从那里看过去
希望对您有所帮助。
关于java - 更改屏幕方向时重置操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14768542/
如何正确创建Rails迁移,以便将表更改为MySQL中的MyISAM?目前是InnoDB。运行原始执行语句会更改表,但它不会更新db/schema.rb,因此当在测试环境中重新创建表时,它会返回到InnoDB并且我的全文搜索失败。我如何着手更改/添加迁移,以便将现有表修改为MyISAM并更新schema.rb,以便我的数据库和相应的测试数据库得到相应更新? 最佳答案 我没有找到执行此操作的好方法。您可以像有人建议的那样更改您的schema.rb,然后运行:rakedb:schema:load,但是,这将覆盖您的数据。我的做法是(假设
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
我尝试使用不同的ssh_options在同一阶段运行capistranov.3任务。我的production.rb说:set:stage,:productionset:user,'deploy'set:ssh_options,{user:'deploy'}通过此配置,capistrano与用户deploy连接,这对于其余的任务是正确的。但是我需要将它连接到服务器中配置良好的an_other_user以完成一项特定任务。然后我的食谱说:...taskswithoriginaluser...task:my_task_with_an_other_userdoset:user,'an_othe
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
假设我有一个FireNinja我的数据库中的对象,使用单表继承存储。后来才知道他真的是WaterNinja.将他更改为不同的子类的最干净的方法是什么?更好的是,我很想创建一个新的WaterNinja对象并替换旧的FireNinja在数据库中,保留ID。编辑我知道如何创建新的WaterNinja来self现有FireNinja的对象,我也知道我可以删除旧的并保存新的。我想做的是改变现有项目的类别。我是通过创建一个新对象并执行一些ActiveRecord魔法来替换行,还是通过对对象本身做一些疯狂的事情,或者甚至通过删除它并使用相同的ID重新插入来做到这一点,这是问题的一部分。
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
相信很多人在录制视频的时候都会遇到各种各样的问题,比如录制的视频没有声音。屏幕录制为什么没声音?今天小编就和大家分享一下如何录制音画同步视频的具体操作方法。如果你有录制的视频没有声音,你可以试试这个方法。 一、检查是否打开电脑系统声音相信很多小伙伴在录制视频后会发现录制的视频没有声音,屏幕录制为什么没声音?如果当时没有打开音频录制,则录制好的视频是没有声音的。因此,建议在录制前进行检查。屏幕上没有声音,很可能是因为你的电脑系统的声音被禁止了。您只需打开电脑系统的声音,即可录制音频和图画同步视频。操作方法:步骤1:点击电脑屏幕右下侧的“小喇叭”图案,在上方的选项中,选择“声音”。 步骤2:在“声
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/