2015-10-18 33 views
1

这是我addbudget类如何使我的简单活动作为android-studio中的浮动活动?

public class addbudget extends ActionBarActivity implements View.OnClickListener{ 


      @Override 
      protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.addbudget); 

       btn66=(Button)findViewById(R.id.btn_addBudget); 
       btn66.setOnClickListener(this); 
       helper=new DBhelper(addbudget.this); 
       txr=(TextView)findViewById(R.id.addbud); 

       txtBudget=(EditText)findViewById(R.id.etBudget); 

       list = (ListView) findViewById(R.id.list); 
       list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

        @Override 
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { 

         String h; 
         Cursor row = (Cursor) arg0.getItemAtPosition(position); 
         selected_did = row.getString(0); 
         h = row.getString(1); 

         txtBudget.setText(h); 





        } 

       }); 


       Bundle data_from_list= getIntent().getExtras(); 
       value_in_tv= data_from_list.getString("passed data key"); 
       txr.setText(value_in_tv); 

       fetchData2(); 
      } 




      private void clearfield(){ 
       txtBudget.setText(""); 

      } 


      public void onClick(View v) { 

        if (btn66 == v) { 
         checkIfRowPresent(txr.getText().toString()); 
        } 
       } 

      public boolean checkIfRowPresent(String name) { 
       SQLiteDatabase db = helper.getWritableDatabase(); 

       Cursor cursor = 
         db.query(DBhelper.TABLE2, null, DBhelper.Description + "='" + name + "'", null, null, null, 
           null, null); 
       boolean ret = false; 
       if (cursor.getCount() > 0) { 

        Toast.makeText(this, "Budget not add Successfully", Toast.LENGTH_LONG).show();// There is a row present in table1 with the given name 
       } 
       else{ 


        ContentValues value = new ContentValues(); 
        value.put(DBhelper.Amount, txtBudget.getText().toString()); 
        value.put(DBhelper.Description, txr.getText().toString()); 


        db = helper.getWritableDatabase(); 
        db.insert(DBhelper.TABLE2, null, value); 
        db.close(); 
        clearfield(); 
        Toast.makeText(this, "Budget add Successfully", Toast.LENGTH_LONG).show(); 
        fetchData2(); 
        Intent i = new Intent(addbudget.this, MainActivity.class); 
        startActivity(i); 
       } 
       db.close(); 

       return ret; 

      } 


      private void fetchData2() { 
       db = helper.getReadableDatabase(); 
       Cursor c = db.query(DBhelper.TABLE2, null, DBhelper.Description + "='" + value_in_tv+"'", null, null, null, null); 
       adapter = new SimpleCursorAdapter(
         this, 
         R.layout.addbudget, 
         c, 
         new String[]{DBhelper.Amount}, 
         new int[]{R.id.lbl}); 
       list.setAdapter(adapter); 
      } 

     } 

这是我addbudget.xml

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="25dp" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="20dp" 
      android:text="Category   " 
      android:id="@+id/textView3" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Budget" 
      android:layout_marginTop="20dp" 
      android:id="@+id/textView4" 
      android:layout_below="@+id/spinner_cat" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" /> 

     <Spinner 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/spinner_cat" 
      android:entries="@array/array_categories" 
      android:layout_alignTop="@+id/textView3" 
      android:layout_toRightOf="@+id/textView3" 
      android:layout_toEndOf="@+id/textView3" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:inputType="numberDecimal" 
      android:ems="10" 
      android:id="@+id/etBudget" 
      android:layout_below="@+id/spinner_cat" 
      android:layout_alignLeft="@+id/spinner_cat" 
      android:layout_alignStart="@+id/spinner_cat" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Add Budget" 
      android:id="@+id/btn_addBudget" 

      android:layout_marginTop="30dp" 

      android:layout_below="@+id/etBudget" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:layout_alignRight="@+id/etBudget" 
      android:layout_alignEnd="@+id/etBudget" /> 
    </RelativeLayout> 

在另一个XML布局,我都一个按钮,如果我点击该按钮,这项活动需要开放为浮动活动

我谷歌这个问题,但答案似乎有点tricky.I不明白那么多,谁能帮助我改变浮动活性的上述活动?

回答

1

这是一个分步实施。我会复制粘贴我的代码并解释它。

在你style.xml添加此与您认为合适的

<style name="PopupTheme" parent="Theme.AppCompat.Light.Dialog"> 
    <item name="android:windowIsFloating">false</item> 
    <item name="android:windowContentOverlay">@null</item> 
    <item name="android:windowSoftInputMode">stateAlwaysHidden</item> 
    <item name="windowActionModeOverlay">true</item> 
    <item name="android:windowIsTranslucent">true</item> 

    <item name="android:windowCloseOnTouchOutside">false</item> 
    <item name="android:colorBackgroundCacheHint">@null</item> 
    <item name="android:backgroundDimEnabled">true</item> 
    <item name="colorAccent">@color/color_primary</item> 
</style> 

创建Activity文件,你可以修改它,它也Layout

在你Activity's .class文件,你可以在onAttachWindow Override methods如处理屏幕尺寸:

@Override 
public void onAttachedToWindow() { 
    super.onAttachedToWindow(); 

    int screenSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK; 

    switch (screenSize) { 
     case Configuration.SCREENLAYOUT_SIZE_LARGE: 
      this.getWindow().setLayout(900, 755); 
      break; 
     case Configuration.SCREENLAYOUT_SIZE_XLARGE: 
      this.getWindow().setLayout(1080, 1000); //width x height 
      break; 
    } 
} 

最后,在你的AndroidManifest.xml文件中添加此

<activity android:name=".MyFloatingActivity" 
     android:enabled="true" 
     android:theme="@style/PopupTheme"<--The theme in style 
     android:label="@string/app_name"/> 
+0

错误:(14 21)否找到与给定名称相匹配的资源:attr'windowNoTitle'。它给出了这个错误,我现在需要做什么? – xyz

+0

我将需要调用onAttachedToWindow()方法oncreate? – xyz

+0

再看看答案。我已经编辑它 –