如何使一个成功的活动按钮可见frominvisible happend在另一活动 [英] How to make a button visible frominvisible on a successful event happend in another activity

查看:105
本文介绍了如何使一个成功的活动按钮可见frominvisible happend在另一活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有按钮V1-V8可见光和R1-R8在我activity_main.xml文件不可见的最初

I have buttons V1-V8 visible and R1-R8 invisible initially in my activity_main.xml file

我的MainActivity.java文件

My MainActivity.java file

package com.example.buttonvtor;

import com.example.buttonvtor.MainActivity;
import com.example.buttonvtor.Payment1Activity;
import com.example.buttonvtor.Payment2Activity;
import com.example.buttonvtor.Payment3Activity;
import com.example.buttonvtor.Payment4Activity;
import com.example.buttonvtor.Payment5Activity;
import com.example.buttonvtor.Payment6Activity;
import com.example.buttonvtor.Payment7Activity;
import com.example.buttonvtor.Payment8Activity;
import com.example.buttonvtor.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

Button bv1;
Button bv2;
Button bv3;
Button bv4;
Button bv5;
Button bv6;
Button bv7;
Button bv8;
Button br1;

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

    bv1 = (Button)findViewById(R.id.button1);
    bv2 = (Button)findViewById(R.id.button2);
    bv3 = (Button)findViewById(R.id.button3);
    bv4 = (Button)findViewById(R.id.button4);
    bv5 = (Button)findViewById(R.id.button5);
    bv6 = (Button)findViewById(R.id.button6);
    bv7 = (Button)findViewById(R.id.button7);
    bv8 = (Button)findViewById(R.id.button8);
            br1 = (Button)findViewById(R.id.r1);
    bv1.setOnClickListener(this);
    bv2.setOnClickListener(this);
    bv3.setOnClickListener(this);
    bv4.setOnClickListener(this);
    bv5.setOnClickListener(this);
    bv6.setOnClickListener(this);
    bv7.setOnClickListener(this);
    bv8.setOnClickListener(this);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


public void onClick(View v) {
switch(v.getId()){

case R.id.button1:
Toast.makeText(MainActivity.this,"You clicked on block 1",Toast.LENGTH_LONG).show();
startActivityForResult(new Intent(MainActivity.this,Payment1Activity.class),1);
break;

case R.id.button2:
Toast.makeText(MainActivity.this,"You clicked on block 2",Toast.LENGTH_LONG).show();
startActivityForResult(new Intent(MainActivity.this,Payment2Activity.class),2);
break;

case R.id.button3:
Toast.makeText(MainActivity.this,"You clicked on block 3",Toast.LENGTH_LONG).show();
startActivityForResult(new Intent(MainActivity.this,Payment3Activity.class),3);
break;

case R.id.button4:
Toast.makeText(MainActivity.this,"You clicked on block 4",Toast.LENGTH_LONG).show();
startActivityForResult(new Intent(MainActivity.this,Payment4Activity.class),4);
break;

case R.id.button5:
Toast.makeText(MainActivity.this,"You clicked on block 5",Toast.LENGTH_LONG).show();
startActivityForResult(new Intent(MainActivity.this,Payment5Activity.class),5);
break;

case R.id.button6:
Toast.makeText(MainActivity.this,"You clicked on block 6",Toast.LENGTH_LONG).show();
startActivityForResult(new Intent(MainActivity.this,Payment6Activity.class),6);
break;

case R.id.button7:
Toast.makeText(MainActivity.this,"You clicked on block 7",Toast.LENGTH_LONG).show();
startActivityForResult(new Intent(MainActivity.this,Payment7Activity.class),7);
break;

case R.id.button8:
Toast.makeText(MainActivity.this,"You clicked on block 8",Toast.LENGTH_LONG).show();
startActivityForResult(new Intent(MainActivity.this,Payment8Activity.class),8);
break;
     }

    }

 protected void onActivityResult (int requestCode, int resultCode, Intent data)
  {
    if (requestCode == 1)
    {// here you match the number you sent in startActivityForResult
        if (resultCode == RESULT_OK)
       {
            bv1.setVisibility(View.INVISIBLE);
            br1.setVisibility(0);


        }
             // do something
    }
      }
  }

在我点击按钮,比如V1则进入所谓Payment1Activity.java下一个活动,并检查用户的合法性。如果用户是有效的并且付款​​成功,它回来给我MainActivity.java。

Once I click on say button V1 it goes to the next activity called Payment1Activity.java and checks the users validity. If a user is valid and the payment is successful it comes back to my MainActivity.java.

Payment1Activity.java

Payment1Activity.java

package com.example.buttonvtor;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RemoteViews;


public class Payment1Activity extends Activity {
Button b;
EditText et,pass;
TextView tv;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_payment1);
    b = (Button)findViewById(R.id.Button01);  
    et = (EditText)findViewById(R.id.accountno);
    pass= (EditText)findViewById(R.id.password);
    tv = (TextView)findViewById(R.id.tv);

    b.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View v) {

            dialog = ProgressDialog.show(Payment1Activity.this, "", 
                    "Validating user...", true);
             new Thread(new Runnable() {
                    public void run() {
                        payment();                        
                    }
                  }).start();               
        }
    });
}

void payment(){
try{            

httpclient=new DefaultHttpClient();
httppost= new HttpPost("my url"); 
        //add your data
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("accno",et.getText().toString().trim())); 
nameValuePairs.add(newBasicNameValuePair("bpassword",pass.getText().toString().trim()));                                   
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        //Execute HTTP Post Request
response=httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response); 
runOnUiThread(new Runnable() {
            public void run() {
                tv.setText("Response from PHP : " + response);
                dialog.dismiss();
            }
        });

    if(response.startsWith("User Found")){
runOnUiThread(new Runnable() {
   public void run() {
 Toast.makeText(Payment1Activity.this,"Payment Successful", Toast.LENGTH_SHORT).show();
 Toast.makeText(Payment1Activity.this,"You reserved block 1",Toast.LENGTH_LONG).show(); 
                }
    });
    Intent returnIntent = new Intent();
returnIntent.putExtra("Payment Successful",RESULT_OK);
setResult(RESULT_OK,returnIntent);     
finish();

   startActivity(new Intent(Payment1Activity.this, MainActivity.class));
}else{
            showAlert();                
        }

    }catch(Exception e){
        dialog.dismiss();
        System.out.println("Exception : " + e.getMessage());
    }
}
public void showAlert(){
Payment1Activity.this.runOnUiThread(new Runnable() {
      public void run() {
    AlertDialog.Builder builder = new AlertDialog.Builder(Payment1Activity.this);
builder.setTitle("Payment Error.");
builder.setMessage("User not Found.")  
          .setCancelable(false)
          .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                       }
                   });                     
            AlertDialog alert = builder.create();
            alert.show();               
        }
    });
}
  }

所以,我想要的东西,这样当我Payment1Activity.java说付款成功,并重定向用户到主页面,当时我的V1按钮应该得到无形的,R1应该可见。我尝试了不同的排列组合来实现这一点,但它并没有帮助。请帮忙。任何建议或意见将是非常美联社preciated。谢谢你。

So I want something such that when my Payment1Activity.java says payment successful and redirects a user to main page at that time my V1 Button should get invisible and R1 should get visible. I tried different permutations and combinations to achieve this but it did not help. Please help. Any suggestions or advices will be highly appreciated. Thank you.

推荐答案

在您的主要活动中使用 startActivityForResult 而不是 startActivity 。这可以让你从新的活动返回一个值的主要活动。例如:

In your main Activity use startActivityForResult instead of startActivity. This allows you to return a value from the new Activity to the Main Activity. For example:

在主要活动

startActivityForResult(new Intent(MainActivity.this,Payment3Activity.class), 3);

下面,3号是一个数字定义,让您以后识别结果。在您的付款#的活动,你将不得不在某个时候(例如,当您disimiss你的Activity)

Here, number 3 is a number you define that allows you to identify the result later. In your Payment#Activity you would have at some point (for example when you disimiss your Activity)

setResult(RESULT_OK); // or any result you want

结果发送到MainActivity,你必须重写 onActivityResult()得到的结果。

protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
    if (requestCode == 3) // here you match the number you sent in startActivityForResult
        if (resultcode == RESULT_OK)
             // do something
}

这篇关于如何使一个成功的活动按钮可见frominvisible happend在另一活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆